Skip to content

Commit

Permalink
remove provide-image, replace with arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilgardis committed Jun 12, 2022
1 parent e22075d commit f98a6e6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 50 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ RUN dpkg --add-architecture arm64 && \
apt-get install --assume-yes libfoo:arm64
```

If you want cross to provide the `FROM` instruction, use `target.{{TARGET}}.dockerfile.provide-base`.
If you want cross to provide the `FROM` instruction, you can do the following

``` Dockerfile
ARG CROSS_BASE_IMAGE
FROM $CROSS_BASE_IMAGE

RUN ...
```

#### Pre-build hook

Expand Down
1 change: 0 additions & 1 deletion docs/cross_toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ passthrough = ["IMPORTANT_ENV_VARIABLES"]
file = "./Dockerfile"
context = "."
build-args = { ARG1 = "foo" }
provide-base = false # tell cross to insert a `FROM` instruction
```
13 changes: 0 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ impl Environment {
self.get_target_var(target, "DOCKERFILE")
}

fn dockerfile_provide_base(&self, target: &Target) -> Option<bool> {
self.get_target_var(target, "DOCKERFILE_PROVIDE_BASE")
.map(|s| bool_from_envvar(&s))
}

fn dockerfile_context(&self, target: &Target) -> Option<String> {
self.get_target_var(target, "DOCKERFILE_CONTEXT")
}
Expand Down Expand Up @@ -248,14 +243,6 @@ impl Config {
self.string_from_config(target, Environment::dockerfile, CrossToml::dockerfile)
}

pub fn dockerfile_provide_base(&self, target: &Target) -> Option<bool> {
self.bool_from_config(
target,
|s, t| (None, s.dockerfile_provide_base(t)),
|s, t| (None, s.dockerfile_provide_base(t)),
)
}

pub fn dockerfile_context(&self, target: &Target) -> Result<Option<String>> {
self.string_from_config(
target,
Expand Down
13 changes: 0 additions & 13 deletions src/cross_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ pub struct CrossTargetConfig {
#[serde(rename_all = "kebab-case")]
pub struct CrossTargetDockerfileConfig {
file: String,
#[serde(default)]
provide_base: bool,
context: Option<String>,
build_args: Option<HashMap<String, String>>,
}
Expand All @@ -57,7 +55,6 @@ impl FromStr for CrossTargetDockerfileConfig {
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(CrossTargetDockerfileConfig {
file: s.to_string(),
provide_base: false,
context: None,
build_args: None,
})
Expand Down Expand Up @@ -104,16 +101,6 @@ impl CrossToml {
self.get_string(target, |t| t.dockerfile.as_ref().map(|c| &c.file))
}

/// Returns the `target.{}.dockerfile.provide_base` part of `Cross.toml`
pub fn dockerfile_provide_base(&self, target: &Target) -> Option<bool> {
self.get_bool(
target,
|_| None,
|t| t.dockerfile.as_ref().map(|c| c.provide_base),
)
.1
}

/// Returns the `target.{}.dockerfile.context` part of `Cross.toml`
pub fn dockerfile_context(&self, target: &Target) -> Option<String> {
self.get_string(target, |t| {
Expand Down
35 changes: 15 additions & 20 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,23 +360,15 @@ pub fn run(
let context = config.dockerfile_context(target)?;
let name = config.image(target)?;

let mut build = Dockerfile::File {
let build = Dockerfile::File {
path: &path,
context: context.as_deref(),
name: name.as_deref(),
};

if config.dockerfile_provide_base(target).unwrap_or_default() {
let mut content = format!("FROM {image}\n");
content.push_str(&crate::file::read(&path)?);
build = Dockerfile::Custom {
content,
from_file: true,
};
}

image = build
.build(
config,
&engine,
&host_root,
config.dockerfile_build_args(target)?.unwrap_or_default(),
Expand All @@ -397,10 +389,10 @@ ARG CROSS_DEB_ARCH=
ARG CROSS_CMD
RUN eval "${{CROSS_CMD}}""#
),
from_file: false,
};
custom
.build(
config,
&engine,
&host_root,
Some(("CROSS_CMD", pre_build.join("\n"))),
Expand Down Expand Up @@ -428,14 +420,14 @@ pub enum Dockerfile<'a> {
},
Custom {
content: String,
/// if `true`, the name of the file will be slightly different
from_file: bool,
},
}

impl<'a> Dockerfile<'a> {
#[allow(clippy::too_many_arguments)]
pub fn build(
&self,
config: &Config,
engine: &Path,
host_root: &Path,
build_args: impl IntoIterator<Item = (impl AsRef<str>, impl AsRef<str>)>,
Expand All @@ -454,20 +446,23 @@ impl<'a> Dockerfile<'a> {
}

if let Some(arch) = target_triple.deb_arch() {
docker_build.args(["--build-arg", &format!("CROSS_DEB_ARCH={}", arch)]);
docker_build.args(["--build-arg", &format!("CROSS_DEB_ARCH={arch}")]);
}

if let Ok(cross_base_image) = image(config, target_triple) {
docker_build.args([
"--build-arg",
&format!("CROSS_BASE_IMAGE={cross_base_image}"),
]);
}

let path = match self {
Dockerfile::File { path, .. } => PathBuf::from(path),
Dockerfile::Custom { content, from_file } => {
Dockerfile::Custom { content } => {
let path = host_root
.join("target")
.join(target_triple.to_string())
.join(format!(
"Dockerfile.{}{}",
target_triple,
if !from_file { "-custom" } else { "" }
));
.join(format!("Dockerfile.{}", target_triple,));
{
let mut file = file::write_file(&path, true)?;
file.write_all(content.as_bytes())?;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ impl Target {
}

/// Returns the architecture name according to `dpkg` naming convention
///
///
/// # Notes
///
///
/// Some of these make no sense to use in our standard images
pub fn deb_arch(&self) -> Option<&'static str> {
match self.triple() {
Expand Down

0 comments on commit f98a6e6

Please sign in to comment.