onelson / jq-rs

Rust crate to provide programmatic access to `jq`.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cargo builld error

yuer1727 opened this issue · comments

commented

when I run " cargo build --release "

error: failed to run custom build command for jq-sys v0.2.2
process didn't exit successfully: /Users/aligame/work/rust_repository/rutils/target/release/build/jq-sys-68771fd250306394/build-script-build (exit code: 101)
--- stderr
thread 'main' panicked at 'Unable to find libjq. Try setting JQ_LIB_DIR to specify the location of the lib.', /Users/aligame/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/jq-sys-0.2.2/build.rs:110:13
note: Run with RUST_BACKTRACE=1 environment variable to display a backtrace.

my code:
`

        println!("file path:{}", file_path);
        println!("pattern:{}", pattern);
        let file = File::open(&file_path).unwrap();
        let buf_reader = BufReader::new(file);
        for line in buf_reader.lines(){
            let line = line.unwrap();
            let output = jq_rs::run(&pattern, &line).unwrap();
            println!("{}", output);
        }

`

Seems as though the crate wasn't able to locate libjq. Locating the library can be handled automatically if pkg-config knows where to find it, but this isn't always the case (it depends on your system and how you installed or build libjq).

Take a look at https://github.com/onelson/jq-sys#building for info on how to set env vars to hint the location of the c library, or otherwise you could try to enable the bundled feature which will attempt to compile libjq from source for you at compile time.

The bundled feature is a little flaky so your mileage may vary.

commented

Is there any way for me to get libjq? I believe it to be more expedient to ask. I have been trying to build the with the 'bundled' feature to no avail (jq-src compiles fine but build fails on jq-sys) and I can't find this libjq in any of the jq projects.

@DoisKoh I'd be interested to see the output from your build failure.

libjq is a part of jq itself. If you want to build libjq yourself, you can do this by getting the jq source from github.

Something like this might work:

$ git clone https://github.com/stedolan/jq.git
$ cd jq
$ git pull --tags
$ git checkout tags/jq-1.6
$ git submodule update --init
$ autoreconf -i
$ ./configure --with-oniguruma=builtin --disable-maintainer-mode --prefix=/usr/local/
$ make LDFLAGS=-all-static
$ sudo make install

# then in your project ...
$ export JQ_LIB_DIR=/usr/local/lib
$ export JQ_LIB_STATIC=1
$ cargo build

If you don't want to build the lib yourself, you might be able to get it from a package manager for your OS, though this crate only works with v1.6 and at the time of writing (late 2018) Linux distros seemed to only offer v1.5. I'm not sure what is available for MacOS or Windows.

commented

Hi and thanks for taking the time to respond.

Building on Windows Subsystem for Linux (Ubuntu 20.04 LTS), the build fails at:

...5/9: jq-sys(build)

Followed by many warnings, example:

warning: cast between incompatible function types from ‘jv (*)(jq_state *, jv)’ {aka ‘struct <anonymous> (*)(struct jq_state *, struct <anonymous>)’} to ‘void (*)()’ [-Wcast-function-type]
   1653 |   {(cfunction_ptr)f_current_filename, "input_filename", 1},
        |    ^

And finally:

src/lexer.c:1500:47: warning: comparison of integer expressions of different signedness: ‘int’ and ‘yy_size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
  make[1]: *** [Makefile:1104: install-recursive] Error 1
  make: *** [Makefile:1612: install] Error 2
  thread '<unnamed>' panicked at '
  command did not execute successfully, got: exit status: 2

  build script failed, must exit now', /home/dois/.cargo/registry/src/github.com-1ecc6299db9ec823/autotools-0.2.3/src/lib.rs:595:5
  thread 'main' panicked at 'autotools build: ()', /home/dois/.cargo/registry/src/github.com-1ecc6299db9ec823/jq-sys-0.2.2/build.rs:8:21

Anyway, I've got it running by getting libjq from compiling the original source. Thanks!

Got it. Thanks for the detail!

The relationship between jq-sys and jq-src is a little interesting in so much as jq-src doesn't do anything itself, but rather encapsulates how to build jq (much like you did manually). When jq-sys fails for you, it's actually failing on work done by jq-src. It's confusing because it inverts build-time and runtime (sort of).

I've had issues with the crates used to perform the build in code - I don't know how better to characterize it besides the autotools build seeming flaky (sometimes works, sometimes doesn't). Ref: onelson/jq-src#1

Since some time has passed, I'll aim to revisit the jq-src crate to see if maybe an update to a more recent autotools version could help make it more reliable. Still, since the failures have been intermittent (for me) it may be hard know when we've finally got a good solution! A proper solve may require unwinding the jq build entirely so we can avoid autotools.

commented

By the way, I never got my Rust program with a jq-rs depedency to build on Windows... I can use the libjq.a for Ubuntu, but I always get linker errors when I try it on Windows. I've tried cross-compiling from Ubuntu; targeting Windows. I can build the .exe and it works but the libs don't. I'm not too familiar with C++ toolchains and building with them... if anyone ever manages to get this to work on Windows, please share! Thank you. I've given up and decided to build on Ubuntu for now.

@DoisKoh ugh, sorry to hear this. I've never attempted a Windows build so I don't have any guidance for this at the moment.
Hopefully, someone out there who lives in the Windows space has good advice to share. I tend to live in Linux, myself.