From 06ef2ff403f714654feb5b4267e7690317465580 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Thu, 14 Aug 2025 00:46:55 +0100 Subject: [PATCH 1/8] feat: add build options to build neither tests nor tools Signed-off-by: Chris Chan --- CMakeLists.txt | 9 ++++-- lib/CMakeLists.txt | 6 ++-- rust/flake.lock | 0 rust/flake.nix | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 rust/flake.lock create mode 100644 rust/flake.nix diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e62fe19..a788db70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,9 @@ if(ccache_executable) set(CMAKE_CXX_COMPILER_LAUNCHER ${ccache_executable}) endif() +option(BUILD_TESTS "Build the tests" OFF) +option(BUILD_TOOLS "Build the tools" ON) + # Enable testing for the project enable_testing() @@ -54,8 +57,10 @@ if(APPLE) endif() add_subdirectory(lib) -add_subdirectory(tools) add_subdirectory(utils) +if (BUILD_TOOLS) + add_subdirectory(tools) +endif() find_package(Doxygen) @@ -132,4 +137,4 @@ set(CPACK_PACKAGE_CONTACT "DMF MXL ") set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}") # Include CPack -include(CPack) \ No newline at end of file +include(CPack) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 14bcdb74..d716dcbd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -89,7 +89,9 @@ target_link_libraries(mxl # Alias trace to libtrace::libtrace so that this library can be used # in lieu of a module from the local source tree add_library(${PROJECT_NAME}::mxl ALIAS mxl) -add_subdirectory(tests) +if (BUILD_TESTS) + add_subdirectory(tests) +endif() # Install targets install(TARGETS mxl EXPORT ${PROJECT_NAME}-targets @@ -162,4 +164,4 @@ install(EXPORT ${PROJECT_NAME}-targets NAMESPACE ${PROJECT_NAME}:: DESTINATION ${MXL_CMAKE_CONFIG_DESTINATION} COMPONENT ${PROJECT_NAME}-dev - ) \ No newline at end of file + ) diff --git a/rust/flake.lock b/rust/flake.lock new file mode 100644 index 00000000..e69de29b diff --git a/rust/flake.nix b/rust/flake.nix new file mode 100644 index 00000000..a219b532 --- /dev/null +++ b/rust/flake.nix @@ -0,0 +1,70 @@ +{ + description = "Flake for MXL dev"; + + inputs = { + nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/*.tar.gz"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { + self, + nixpkgs, + rust-overlay + }: let + overlays = [ + (import rust-overlay) + (self: super: { + rustStable = super.rust-bin.stable."1.88.0".default; + rustNightly = super.rust-bin.nightly."2025-06-26".default; + }) + ]; + + allSystems = [ + "x86_64-linux" # 64-bit Intel/AMD Linux + "aarch64-linux" # 64-bit ARM Linux + "x86_64-darwin" # 64-bit Intel macOS + "aarch64-darwin" # 64-bit ARM macOS + ]; + + forAllSystems = f: + nixpkgs.lib.genAttrs allSystems (system: + f { + pkgs = import nixpkgs { + inherit overlays system; + }; + } + ); + in { + devShells = forAllSystems ({pkgs}: { + default = pkgs.mkShell { + LIBCLANG_PATH = pkgs.lib.makeLibraryPath [pkgs.llvmPackages_latest.libclang.lib]; + packages = + (with pkgs; [ + rustStable + rust-analyzer + clang + cmake + pkg-config + ]); + }; + } + ); + nightly = forAllSystems ({pkgs}: { + default = pkgs.mkShell { + LIBCLANG_PATH = pkgs.lib.makeLibraryPath [pkgs.llvmPackages_latest.libclang.lib]; + packages = + (with pkgs; [ + rustNightly + rust-analyzer + clang + cmake + pkg-config + ]); + }; + } + ); + }; +} From 41c7b9422a40d3bce9ac18f6b54a05480a2a3fe7 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Thu, 14 Aug 2025 00:50:16 +0100 Subject: [PATCH 2/8] feat: ignore generated mxl version header file Signed-off-by: Chris Chan --- rust/mxl-sys/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 rust/mxl-sys/.gitignore diff --git a/rust/mxl-sys/.gitignore b/rust/mxl-sys/.gitignore new file mode 100644 index 00000000..8d6e3400 --- /dev/null +++ b/rust/mxl-sys/.gitignore @@ -0,0 +1 @@ +mxl/version.h From 1bcbec72912009b0c8cc37e88faae59a840ca500 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Thu, 14 Aug 2025 14:22:20 +0100 Subject: [PATCH 3/8] feat: add options to not build tools nor tests Signed-off-by: Chris Chan --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a788db70..72b11e1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,9 @@ else() string(APPEND mxl_VERSION ".0") endif() +option(BUILD_TESTS "Build the tests" ON) +option(BUILD_TOOLS "Build the tools" ON) + project(mxl VERSION ${mxl_VERSION} LANGUAGES CXX C @@ -37,9 +40,6 @@ if(ccache_executable) set(CMAKE_CXX_COMPILER_LAUNCHER ${ccache_executable}) endif() -option(BUILD_TESTS "Build the tests" OFF) -option(BUILD_TOOLS "Build the tools" ON) - # Enable testing for the project enable_testing() From 44dc78c45bc22c38cba0bf8ea3abe420cde1e598 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Thu, 14 Aug 2025 15:18:11 +0100 Subject: [PATCH 4/8] feat: use `cmake` in `build.rs` for `mxl-sys` Signed-off-by: Chris Chan --- rust/Cargo.lock | 19 +++++++++++++++++++ rust/mxl-sys/Cargo.toml | 1 + rust/mxl-sys/build.rs | 29 +++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 6a731e57..eda5e2d4 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -60,6 +60,15 @@ version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +[[package]] +name = "cc" +version = "1.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2352e5597e9c544d5e6d9c95190d5d27738ade584fa8db0a16e130e5c2b5296e" +dependencies = [ + "shlex", +] + [[package]] name = "cexpr" version = "0.6.0" @@ -124,6 +133,15 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" +[[package]] +name = "cmake" +version = "0.1.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0" +dependencies = [ + "cc", +] + [[package]] name = "dlopen2" version = "0.8.0" @@ -251,6 +269,7 @@ name = "mxl-sys" version = "0.1.0" dependencies = [ "bindgen", + "cmake", ] [[package]] diff --git a/rust/mxl-sys/Cargo.toml b/rust/mxl-sys/Cargo.toml index 6bbb0ffd..0e4fa65a 100644 --- a/rust/mxl-sys/Cargo.toml +++ b/rust/mxl-sys/Cargo.toml @@ -8,6 +8,7 @@ version.workspace = true [build-dependencies] bindgen.workspace = true +cmake = "0.1.54" [features] mxl-not-built = [] diff --git a/rust/mxl-sys/build.rs b/rust/mxl-sys/build.rs index 0d76aff0..625302cc 100644 --- a/rust/mxl-sys/build.rs +++ b/rust/mxl-sys/build.rs @@ -1,4 +1,5 @@ use std::env; +use std::fs; use std::path::PathBuf; #[cfg(debug_assertions)] @@ -27,8 +28,7 @@ fn get_bindgen_specs() -> BindgenSpecs { .to_string_lossy() .to_string(), ]; - #[cfg(not(feature = "mxl-not-built"))] - { + if cfg!(not(feature = "mxl-not-built")) { let build_dir = repo_root.join("build").join(BUILD_VARIANT); let build_version_dir = build_dir .join("lib") @@ -51,6 +51,31 @@ fn main() { println!("cargo:include={include_dir}"); } + if cfg!(not(feature = "mxl-not-built")) { + // TODO: figure out when this has to be rebuilt + let mxl_version_out_path = + PathBuf::from(&env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set")) + .join("mxl"); + if !fs::exists(&mxl_version_out_path).expect("Error checking if out path exists") { + fs::create_dir(&mxl_version_out_path).expect("Failed to create out path"); + } + let out_path = mxl_version_out_path.join("version.h"); + println!("cargo:rerun-if-changed={}", out_path.display()); + + let dst = cmake::Config::new("../../") + .define("BUILD_TESTS", "OFF") + .define("BUILD_TOOLS", "OFF") + .build(); + + let mxl_version_location = dst.join("include").join("mxl").join("version.h"); + assert!(matches!(std::fs::exists(&mxl_version_location), Ok(true))); + + fs::copy(&mxl_version_location, &out_path).expect("Could copy mxl version"); + + println!("cargo:rustc-link-search={}", dst.join("lib64").display()); + println!("cargo:rustc-link-lib=mxl"); + } + let bindings = bindgen::builder() .clang_args( bindgen_specs From 21ee1a3a0b93ed86c709c45f5e1529655590d9c9 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Thu, 14 Aug 2025 15:23:15 +0100 Subject: [PATCH 5/8] refactor: move code for compiling `mxl` with cmake Signed-off-by: Chris Chan --- rust/mxl-sys/build.rs | 47 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/rust/mxl-sys/build.rs b/rust/mxl-sys/build.rs index 625302cc..ca0eea5d 100644 --- a/rust/mxl-sys/build.rs +++ b/rust/mxl-sys/build.rs @@ -37,32 +37,18 @@ fn get_bindgen_specs() -> BindgenSpecs { .to_string(); includes_dirs.push(build_version_dir); - } - - BindgenSpecs { - header, - includes_dirs, - } -} - -fn main() { - let bindgen_specs = get_bindgen_specs(); - for include_dir in &bindgen_specs.includes_dirs { - println!("cargo:include={include_dir}"); - } - if cfg!(not(feature = "mxl-not-built")) { - // TODO: figure out when this has to be rebuilt - let mxl_version_out_path = - PathBuf::from(&env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set")) - .join("mxl"); - if !fs::exists(&mxl_version_out_path).expect("Error checking if out path exists") { - fs::create_dir(&mxl_version_out_path).expect("Failed to create out path"); + let mxl_version_out_path = manifest_dir.join("mxl"); + if !fs::exists(&mxl_version_out_path) + .expect("Error checking if out path for version header file exists") + { + fs::create_dir(&mxl_version_out_path) + .expect("Failed to create out path for version header file"); } - let out_path = mxl_version_out_path.join("version.h"); - println!("cargo:rerun-if-changed={}", out_path.display()); + let mxl_version_header = mxl_version_out_path.join("version.h"); + println!("cargo:rerun-if-changed={}", mxl_version_header.display()); - let dst = cmake::Config::new("../../") + let dst = cmake::Config::new(repo_root) .define("BUILD_TESTS", "OFF") .define("BUILD_TOOLS", "OFF") .build(); @@ -70,12 +56,25 @@ fn main() { let mxl_version_location = dst.join("include").join("mxl").join("version.h"); assert!(matches!(std::fs::exists(&mxl_version_location), Ok(true))); - fs::copy(&mxl_version_location, &out_path).expect("Could copy mxl version"); + fs::copy(&mxl_version_location, &mxl_version_header) + .expect("Could copy mxl version header"); println!("cargo:rustc-link-search={}", dst.join("lib64").display()); println!("cargo:rustc-link-lib=mxl"); } + BindgenSpecs { + header, + includes_dirs, + } +} + +fn main() { + let bindgen_specs = get_bindgen_specs(); + for include_dir in &bindgen_specs.includes_dirs { + println!("cargo:include={include_dir}"); + } + let bindings = bindgen::builder() .clang_args( bindgen_specs From 9935f8905ca3206f5c48cf8cbae28211c44435dd Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Fri, 15 Aug 2025 09:42:46 +0100 Subject: [PATCH 6/8] fix: update `build.rs` to build into build dir Signed-off-by: Chris Chan --- rust/mxl-sys/build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/mxl-sys/build.rs b/rust/mxl-sys/build.rs index ca0eea5d..a4220109 100644 --- a/rust/mxl-sys/build.rs +++ b/rust/mxl-sys/build.rs @@ -49,8 +49,10 @@ fn get_bindgen_specs() -> BindgenSpecs { println!("cargo:rerun-if-changed={}", mxl_version_header.display()); let dst = cmake::Config::new(repo_root) + .out_dir("build_dir") .define("BUILD_TESTS", "OFF") .define("BUILD_TOOLS", "OFF") + .configure_arg(format!("--preset={BUILD_VARIANT}")) .build(); let mxl_version_location = dst.join("include").join("mxl").join("version.h"); From 94fa43e32a3e897dd691169ebd669da4bde5a5e7 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Fri, 15 Aug 2025 09:49:16 +0100 Subject: [PATCH 7/8] fix: supply build_dir correctly as path Signed-off-by: Chris Chan --- rust/mxl-sys/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/mxl-sys/build.rs b/rust/mxl-sys/build.rs index a4220109..90ab1b81 100644 --- a/rust/mxl-sys/build.rs +++ b/rust/mxl-sys/build.rs @@ -49,7 +49,7 @@ fn get_bindgen_specs() -> BindgenSpecs { println!("cargo:rerun-if-changed={}", mxl_version_header.display()); let dst = cmake::Config::new(repo_root) - .out_dir("build_dir") + .out_dir(build_dir) .define("BUILD_TESTS", "OFF") .define("BUILD_TOOLS", "OFF") .configure_arg(format!("--preset={BUILD_VARIANT}")) From 38a76004e45a5ad0c552a51ed5c17e8e987b5942 Mon Sep 17 00:00:00 2001 From: Chris Chan Date: Fri, 15 Aug 2025 14:48:49 +0100 Subject: [PATCH 8/8] fix: building the shared library for the rust bindings Signed-off-by: Chris Chan --- rust/mxl-sys/build.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/mxl-sys/build.rs b/rust/mxl-sys/build.rs index 90ab1b81..390ce970 100644 --- a/rust/mxl-sys/build.rs +++ b/rust/mxl-sys/build.rs @@ -47,12 +47,14 @@ fn get_bindgen_specs() -> BindgenSpecs { } let mxl_version_header = mxl_version_out_path.join("version.h"); println!("cargo:rerun-if-changed={}", mxl_version_header.display()); + // TODO: re-run on build_dir changing? let dst = cmake::Config::new(repo_root) .out_dir(build_dir) + .generator("Unix Makefiles") .define("BUILD_TESTS", "OFF") .define("BUILD_TOOLS", "OFF") - .configure_arg(format!("--preset={BUILD_VARIANT}")) + // .configure_arg(format!("--preset={BUILD_VARIANT}")) .build(); let mxl_version_location = dst.join("include").join("mxl").join("version.h");