Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cross-compilation possible #40

Merged
merged 4 commits into from
Jun 17, 2022
Merged

Make cross-compilation possible #40

merged 4 commits into from
Jun 17, 2022

Conversation

spruce
Copy link
Contributor

@spruce spruce commented May 31, 2022

This is achieved by using specific env_vars for the target.
This allows the crate being compiled for diesel_migrations for the host.
While also compiling it with the correct lib dir for the target.

I use this version (including the fixes of #28) of the build.rs to make cross compilation from a M1 Mac to x86_64 and it works great. 👍

This is achieved by using specific env_vars for the target.
This allows the crate being compiled for diesel_migrations for the host.
While also compiling it with the correct lib dir for the target.
Copy link
Collaborator

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be fine with such a change if the two comments are addressed and if some documentation about this is added to the readme.

build.rs Outdated
Comment on lines 88 to 92
let path = PathBuf::from(&pg_lib_path);
if !path.exists() {
panic!("Folder {:?} doesn't exist in the configured path: {:?}", pq_lib_dir_for_target, path);
}
println!("{:?} = {:?}", pq_lib_dir_for_target, pg_lib_path); // list in output for small debuggability
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not performed for the "normal" PQ_LIB_DIR variable at all. Either we need to change that there or remove it from here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to the "normal" variable as well. As I find it to be good practice to give as many hints as possible as to why something fails.

build.rs Outdated
Comment on lines 96 to 98
println!("cargo:rerun-if-env-changed=PQ_LIB_DIR");
println!("PQ_LIB_DIR = {:?}", env::var("PQ_LIB_DIR"));
env::var("PQ_LIB_DIR")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to unify this block with that one below as they are identical.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored it into a function

@clux
Copy link

clux commented Jun 5, 2022

I use this version (including the fixes of #28) of the build.rs to make cross compilation from a M1 Mac to x86_64 and it works great. +1

It doesn't maintain the static linkage fixes from #28 - there are still linking errors without these two lines: https://github.com/sgrif/pq-sys/pull/28/files#diff-d0d98998092552a1d3259338c2c71e118a5b8343dd4703c0c7f552ada7f9cb42R111-R112 (which is probably out of scope for this PR, but just adding a note so #28 isn't closed).

Copy link
Collaborator

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. I've added a few comments about how this could be made to share even more code between the different variants and added a note about the readme entry. If these are fixed this should be ready for merging.

README.md Outdated
Comment on lines 16 to 18
* First, if the environment variable `PQ_LIB_DIR` is set, it will use its value
* Second it will look for an environment variable in the format of `PQ_LIB_DIR_{TARGET}`
where `{TARGET}` gets replaced by the Target environment variable set for cross-compilation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be switched, as the current implementation prefers PQ_LIB_DIR_{TARGET} over PQ_LIB_DIR (which is fine).

build.rs Outdated
@@ -127,6 +146,19 @@ fn configured_by_vcpkg() -> bool {
false
}

fn use_general_lib_dir() -> Result<String, VarError>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change this function in such a way that it accepts the name of a environment variable as input. This would enable reusing the same function for target specific variables as well and unify even more code there.

build.rs Outdated
Comment on lines 86 to 97
println!("cargo:rerun-if-env-changed={}", pq_lib_dir_for_target);
if let Ok(pg_lib_path) = env::var(pq_lib_dir_for_target.clone()) {
let path = PathBuf::from(&pg_lib_path);
if !path.exists() {
panic!("Folder {:?} doesn't exist in the configured path: {:?}", pq_lib_dir_for_target, path);
}
println!("{:?} = {:?}", pq_lib_dir_for_target, pg_lib_path); // list in output for small debuggability
Ok(pg_lib_path)
}
else{
use_general_lib_dir()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the generalized version of use_general_lib_dir() this could be written as

Suggested change
println!("cargo:rerun-if-env-changed={}", pq_lib_dir_for_target);
if let Ok(pg_lib_path) = env::var(pq_lib_dir_for_target.clone()) {
let path = PathBuf::from(&pg_lib_path);
if !path.exists() {
panic!("Folder {:?} doesn't exist in the configured path: {:?}", pq_lib_dir_for_target, path);
}
println!("{:?} = {:?}", pq_lib_dir_for_target, pg_lib_path); // list in output for small debuggability
Ok(pg_lib_path)
}
else{
use_general_lib_dir()
}
use_general_lib_dir(pq_lib_dir_for_target).or_else(|_| use_general_lib_dir("PQ_LIB_DIR"))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have to get used to more functional programming and not be as imperativ as I used to. Thanks for showing me that 👍

@weiznich weiznich merged commit c82aa22 into sgrif:master Jun 17, 2022
@weiznich
Copy link
Collaborator

Thanks for the update. Looks good now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants