Skip to content
New issue

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

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

Already on GitHub? # to your account

secp256k1-sys v0.10.1 fails to compile on thumbv7em-none-eabi target #766

Open
jsdw opened this issue Nov 20, 2024 · 5 comments
Open

secp256k1-sys v0.10.1 fails to compile on thumbv7em-none-eabi target #766

jsdw opened this issue Nov 20, 2024 · 5 comments

Comments

@jsdw
Copy link

jsdw commented Nov 20, 2024

When I try to compile a crate including secp256k1 with cargo build --target thumbv7em-none-eabi, I run into the error printed below.

is it expected that this crate will fail to compile on this target? Perhaps you've encountered this issue before and, if so, perhaps you might have pointers on how to avoid it?

warning: secp256k1-sys@0.10.1: depend/secp256k1/contrib/lax_der_parsing.c:7:10: fatal error: string.h: No such file or directory
warning: secp256k1-sys@0.10.1:     7 | #include <string.h>
warning: secp256k1-sys@0.10.1:       |          ^~~~~~~~~~
warning: secp256k1-sys@0.10.1: compilation terminated.
warning: secp256k1-sys@0.10.1: In file included from depend/secp256k1/src/util.h:13,
warning: secp256k1-sys@0.10.1:                  from depend/secp256k1/src/field.h:10,
warning: secp256k1-sys@0.10.1:                  from depend/secp256k1/src/group.h:10,
warning: secp256k1-sys@0.10.1:                  from depend/secp256k1/src/precomputed_ecmult_gen.c:3:
warning: secp256k1-sys@0.10.1: /opt/homebrew/Cellar/arm-none-eabi-gcc/14.2.0/lib/gcc/arm-none-eabi/14.2.0/include/stdint.h:9:16: fatal error: stdint.h: No such file or directory
warning: secp256k1-sys@0.10.1:     9 | # include_next <stdint.h>
warning: secp256k1-sys@0.10.1:       |                ^~~~~~~~~~
warning: secp256k1-sys@0.10.1: compilation terminated.
error: failed to run custom build command for `secp256k1-sys v0.10.1`

Caused by:
  process didn't exit successfully: `/Users/jameswilson/Work/subxt/testing/no-std-tests/target/debug/build/secp256k1-sys-7af7af88b98c083c/build-script-build` (exit status: 1)
  --- stdout
  OUT_DIR = Some(/Users/jameswilson/Work/subxt/testing/no-std-tests/target/thumbv7em-none-eabi/debug/build/secp256k1-sys-f6a905ec8c4a28e7/out)
  OPT_LEVEL = Some(0)
  TARGET = Some(thumbv7em-none-eabi)
  HOST = Some(aarch64-apple-darwin)
  cargo:rerun-if-env-changed=CC_thumbv7em-none-eabi
  CC_thumbv7em-none-eabi = None
  cargo:rerun-if-env-changed=CC_thumbv7em_none_eabi
  CC_thumbv7em_none_eabi = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CROSS_COMPILE
  CROSS_COMPILE = None
  RUSTC_LINKER = None
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  RUSTC_WRAPPER = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some(true)
  CARGO_CFG_TARGET_FEATURE = Some(dsp,mclass,thumb-mode,thumb2,v5te,v6,v6k,v6t2,v7)
  cargo:rerun-if-env-changed=CFLAGS_thumbv7em-none-eabi
  CFLAGS_thumbv7em-none-eabi = None
  cargo:rerun-if-env-changed=CFLAGS_thumbv7em_none_eabi
  CFLAGS_thumbv7em_none_eabi = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  OUT_DIR = Some(/Users/jameswilson/Work/subxt/testing/no-std-tests/target/thumbv7em-none-eabi/debug/build/secp256k1-sys-f6a905ec8c4a28e7/out)
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  TARGET = Some(thumbv7em-none-eabi)
  CARGO_CFG_TARGET_FEATURE = Some(dsp,mclass,thumb-mode,thumb2,v5te,v6,v6k,v6t2,v7)
  HOST = Some(aarch64-apple-darwin)
  cargo:rerun-if-env-changed=CFLAGS_thumbv7em-none-eabi
  CFLAGS_thumbv7em-none-eabi = None
  cargo:rerun-if-env-changed=CFLAGS_thumbv7em_none_eabi
  CFLAGS_thumbv7em_none_eabi = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  OUT_DIR = Some(/Users/jameswilson/Work/subxt/testing/no-std-tests/target/thumbv7em-none-eabi/debug/build/secp256k1-sys-f6a905ec8c4a28e7/out)
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  TARGET = Some(thumbv7em-none-eabi)
  CARGO_CFG_TARGET_FEATURE = Some(dsp,mclass,thumb-mode,thumb2,v5te,v6,v6k,v6t2,v7)
  HOST = Some(aarch64-apple-darwin)
  cargo:rerun-if-env-changed=CFLAGS_thumbv7em-none-eabi
  CFLAGS_thumbv7em-none-eabi = None
  cargo:rerun-if-env-changed=CFLAGS_thumbv7em_none_eabi
  CFLAGS_thumbv7em_none_eabi = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:warning=depend/secp256k1/contrib/lax_der_parsing.c:7:10: fatal error: string.h: No such file or directory
  cargo:warning=    7 | #include <string.h>
  cargo:warning=      |          ^~~~~~~~~~
  cargo:warning=compilation terminated.
  cargo:warning=In file included from depend/secp256k1/src/util.h:13,
  cargo:warning=                 from depend/secp256k1/src/field.h:10,
  cargo:warning=                 from depend/secp256k1/src/group.h:10,
  cargo:warning=                 from depend/secp256k1/src/precomputed_ecmult_gen.c:3:
  cargo:warning=/opt/homebrew/Cellar/arm-none-eabi-gcc/14.2.0/lib/gcc/arm-none-eabi/14.2.0/include/stdint.h:9:16: fatal error: stdint.h: No such file or directory
  cargo:warning=    9 | # include_next <stdint.h>
  cargo:warning=      |                ^~~~~~~~~~
  cargo:warning=compilation terminated.

  --- stderr


  error occurred: Command "arm-none-eabi-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "-mthumb" "-march=armv7e-m" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-Wno-unused-function" "-Wno-unused-parameter" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-Dprintf(...)=" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-DENABLE_MODULE_RECOVERY=1" "-o" "/Users/jameswilson/Work/subxt/testing/no-std-tests/target/thumbv7em-none-eabi/debug/build/secp256k1-sys-f6a905ec8c4a28e7/out/c928380082b86401-precomputed_ecmult_gen.o" "-c" "depend/secp256k1/src/precomputed_ecmult_gen.c" with args arm-none-eabi-gcc did not execute successfully (status code exit status: 1).
@apoelstra
Copy link
Member

I think this should be fixed by #663.

@apoelstra
Copy link
Member

Ah, no, this is different from #663. This is about string.h in lax_der_parsing. Interesting.

This standards page says that a freestanding implementation must have stdint.h; possibly your compiler does not support C99. But string.h is not required and in theory even a confirming C99 compiler might not have it (but we use e.g. memcpy throughout the codebase, so it would be quite invasive to try to fix this).

Possibly there is way to configure your compiler to provide paths to standard headers? For the things we're doing in this library you could maybe also define your own versions somewhere.

@bigspider
Copy link

This seems the same situation I mentioned in #585 for riscv targets. I still don't know what's the clean solution, but patching the build.rs of secp256k1-sys as follows fixed it for me:

diff --git a/secp256k1-sys/build.rs b/secp256k1-sys/build.rs
index 49d02e3..072ec71 100644
--- a/secp256k1-sys/build.rs
+++ b/secp256k1-sys/build.rs
@@ -12,13 +12,31 @@
 extern crate cc;
 
 use std::env;
+use std::process::Command;
 
 fn main() {
+    let output = Command::new("riscv64-unknown-elf-gcc")
+        .arg("-print-sysroot")
+        .output()
+        .ok();
+    let sysroot = output
+        .as_ref()
+        .and_then(|o| std::str::from_utf8(&o.stdout).ok())
+        .unwrap_or("")
+        .trim();
+
+    let gcc_toolchain = if sysroot.is_empty() {
+        String::from("/usr/include/")
+    } else {
+        format!("{sysroot}/include")
+    };
+
     // Actual build
     let mut base_config = cc::Build::new();
     base_config.include("depend/secp256k1/")
                .include("depend/secp256k1/include")
                .include("depend/secp256k1/src")
+               .include(gcc_toolchain)
                .flag_if_supported("-Wno-unused-function") // some ecmult stuff is defined but not used upstream
                .flag_if_supported("-Wno-unused-parameter") // patching out printf causes this warning
                .define("SECP256K1_API", Some(""))

@TheBlueMatt
Copy link
Member

That sounds like a bug either in the cc crate, or more realistically in your cc. Generally a C compiler should include its systole by default. Maybe try a different distro?

@apoelstra
Copy link
Member

Alternately I wonder if cc will respect you invoking the build process with CFLAGS=-I/usr/include cargo build or something.

If not I wouldn't mind adding some extra env variables to our build.rs (e.g. RUST_SECP256K1_EXTRA_CFLAGS if they were general enough.

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

No branches or pull requests

4 participants