From 214337387f767ff8839fcd795078970acf068b50 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Thu, 2 Jul 2020 11:36:38 +0800 Subject: [PATCH] cc: fixed error and compiled unlzma using cc --- src/Cargo.lock | 1 + src/libc/build.rs | 10 ++++--- src/libunwind/build.rs | 68 ++++++++++++++++++++++++++++++------------ src/szl/Cargo.toml | 3 ++ src/szl/build.rs | 44 +++++++++++++++++---------- 5 files changed, 87 insertions(+), 39 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index e257f1d..1360da5 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -430,6 +430,7 @@ dependencies = [ name = "szl" version = "0.1.0" dependencies = [ + "cc", "cstr_core", "libboard_zynq", "libcortex_a9", diff --git a/src/libc/build.rs b/src/libc/build.rs index e7492b5..43ab8e6 100644 --- a/src/libc/build.rs +++ b/src/libc/build.rs @@ -7,16 +7,18 @@ mod libc { use std::path::Path; pub fn compile() { let cfg = &mut cc::Build::new(); + cfg.no_default_flags(true); + cfg.compiler("clang"); cfg.cpp(false); cfg.warnings(false); - // still have problem compiling the libunwind cfg.flag("-nostdlib"); cfg.flag("-ffreestanding"); cfg.flag("-fno-PIC"); cfg.flag("-isystem../include"); cfg.flag("-fno-stack-protector"); cfg.flag("--target=armv7-none-eabihf"); + cfg.flag("-O2"); cfg.flag("-std=c99"); cfg.flag("-fstrict-aliasing"); @@ -25,12 +27,12 @@ mod libc { cfg.flag("-U_FORTIFY_SOURCE"); cfg.define("_FORTIFY_SOURCE", Some("0")); - let unwind_sources = vec![ + let sources = vec![ "printf.c" ]; - let root = Path::new("../libc"); - for src in unwind_sources { + let root = Path::new("./"); + for src in sources { println!("cargo:rerun-if-changed={}", src); cfg.file(root.join("src").join(src)); } diff --git a/src/libunwind/build.rs b/src/libunwind/build.rs index d907e5f..d38a885 100644 --- a/src/libunwind/build.rs +++ b/src/libunwind/build.rs @@ -1,21 +1,16 @@ fn main() { println!("cargo:rerun-if-changed=build.rs"); - llvm_libunwind::compile(); + llvm_libunwind::compile_cpp(); + llvm_libunwind::compile_c(); } mod llvm_libunwind { use std::path::Path; - /// Compile the libunwind C/C++ source code. - pub fn compile() { - let cfg = &mut cc::Build::new(); - cfg.cpp(true); - cfg.cpp_set_stdlib(None); + fn setup_options(cfg: &mut cc::Build) { + cfg.no_default_flags(true); cfg.warnings(false); - // libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765 - cfg.define("__LITTLE_ENDIAN__", Some("1")); - // still have problem compiling the libunwind cfg.flag("-nostdlib"); cfg.flag("-ffreestanding"); cfg.flag("-fno-PIC"); @@ -23,12 +18,55 @@ mod llvm_libunwind { cfg.flag("-isystem../include"); cfg.flag("-fno-stack-protector"); cfg.flag("--target=armv7-none-eabihf"); + cfg.flag("-O2"); + + cfg.flag("-std=c99"); + cfg.flag("-fstrict-aliasing"); + cfg.flag("-fvisibility=hidden"); + cfg.flag_if_supported("-fvisibility-global-new-delete-hidden"); cfg.define("_LIBUNWIND_IS_BAREMETAL", Some("1")); cfg.define("_LIBUNWIND_NO_HEAP", Some("1")); cfg.define("_LIBUNWIND_HAS_NO_THREADS", Some("1")); cfg.define("NDEBUG", Some("1")); + // libunwind expects a __LITTLE_ENDIAN__ macro to be set for LE archs, cf. #65765 + cfg.define("__LITTLE_ENDIAN__", Some("1")); + cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None); + cfg.flag("-U_FORTIFY_SOURCE"); + cfg.define("_FORTIFY_SOURCE", Some("0")); + } - cfg.flag("-std=c99"); + pub fn compile_c() { + let cfg = &mut cc::Build::new(); + setup_options(cfg); + cfg.compiler("clang"); + + let unwind_sources = vec![ + "Unwind-sjlj.c", + "UnwindLevel1-gcc-ext.c", + "UnwindLevel1.c", + "UnwindRegistersRestore.S", + "UnwindRegistersSave.S", + ]; + + let root = Path::new("../llvm_libunwind"); + cfg.include(root.join("include")); + for src in unwind_sources { + println!("cargo:rerun-if-changed={}", src); + cfg.file(root.join("src").join(src)); + } + + cfg.compile("unwind_c"); + } + + /// Compile the libunwind C/C++ source code. + pub fn compile_cpp() { + let cfg = &mut cc::Build::new(); + setup_options(cfg); + cfg.compiler("clang++"); + cfg.cpp(true); + cfg.cpp_set_stdlib(None); + + // c++ options cfg.flag("-std=c++11"); cfg.flag("-nostdinc++"); cfg.flag("-fno-exceptions"); @@ -37,18 +75,10 @@ mod llvm_libunwind { cfg.flag("-funwind-tables"); cfg.flag("-fvisibility=hidden"); cfg.flag_if_supported("-fvisibility-global-new-delete-hidden"); - cfg.define("_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS", None); - cfg.flag("-U_FORTIFY_SOURCE"); - cfg.define("_FORTIFY_SOURCE", Some("0")); let unwind_sources = vec![ "Unwind-EHABI.cpp", "Unwind-seh.cpp", - "Unwind-sjlj.c", - "UnwindLevel1-gcc-ext.c", - "UnwindLevel1.c", - "UnwindRegistersRestore.S", - "UnwindRegistersSave.S", "libunwind.cpp" ]; @@ -59,6 +89,6 @@ mod llvm_libunwind { cfg.file(root.join("src").join(src)); } - cfg.compile("unwind"); + cfg.compile("unwind_cpp"); } } diff --git a/src/szl/Cargo.toml b/src/szl/Cargo.toml index 9668987..c97c1c3 100644 --- a/src/szl/Cargo.toml +++ b/src/szl/Cargo.toml @@ -15,3 +15,6 @@ cstr_core = { version = "0.2", default-features = false } libboard_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } libsupport_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } libcortex_a9 = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } + +[build-dependencies] +cc = { version = "1.0.1" } diff --git a/src/szl/build.rs b/src/szl/build.rs index 971a291..349572d 100644 --- a/src/szl/build.rs +++ b/src/szl/build.rs @@ -1,28 +1,14 @@ -use std::process::Command; use std::env; use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; fn main() { + println!("cargo:rerun-if-changed=build.rs"); let out = env::var("OUT_DIR").unwrap(); let out_dir = &PathBuf::from(&out); - let status = Command::new("clang") - .args(&["-target", "armv7-unknown-linux", "-fno-stack-protector", - "src/unlzma.c", "-O2", "-c", "-fPIC", "-o", - &format!("{}/unlzma.o", out)]) - .status().unwrap(); - assert!(status.success()); - let status = Command::new("llvm-ar") - .args(&["crus", "libunlzma.a", "unlzma.o"]) - .current_dir(&Path::new(&out)) - .status().unwrap(); - assert!(status.success()); - println!("cargo:rustc-link-search=native={}", out); - println!("cargo:rustc-link-lib=static=unlzma"); - println!("cargo:rerun-if-changed=src/unlzma.c"); - + compile_unlzma(); // Put the linker script somewhere the linker can find it File::create(out_dir.join("link.x")) .unwrap() @@ -34,3 +20,29 @@ fn main() { // instead of when any part of the source code changes. println!("cargo:rerun-if-changed=link.x"); } + +pub fn compile_unlzma() { + let cfg = &mut cc::Build::new(); + cfg.compiler("clang"); + cfg.no_default_flags(true); + cfg.warnings(false); + + cfg.flag("-nostdlib"); + cfg.flag("-ffreestanding"); + cfg.flag("-fPIC"); + cfg.flag("-fno-stack-protector"); + cfg.flag("--target=armv7-unknown-linux"); + cfg.flag("-O2"); + + let sources = vec![ + "unlzma.c", + ]; + + let root = Path::new("./"); + for src in sources { + println!("cargo:rerun-if-changed={}", src); + cfg.file(root.join("src").join(src)); + } + + cfg.compile("unlzma"); +}