skvadrik / re2c

Lexer generator for C, C++, Go and Rust.

Home Page:https://re2c.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

re2c is not relocatable

madebr opened this issue · comments

re2c has an absolute default stdlib include path baked in.
Because of this, you cannot move the re2c install tree if you including files from this folder.

It's added here:

re2c/CMakeLists.txt

Lines 102 to 104 in 6b7a437

set(RE2C_STDLIB_DIR "${CMAKE_INSTALL_PREFIX}/share/re2c/stdlib")
add_compile_definitions(
"RE2C_STDLIB_DIR=\"${RE2C_STDLIB_DIR}\""

And used here:

re2c/src/parse/input.cc

Lines 48 to 51 in 6b7a437

if (!file) {
path = RE2C_STDLIB_DIR + name;
file = fopen(path.c_str(), "rb");
}

Ideally, the CMake script should generate a relative path from the re2c bin directory to the stdlib folder so the following macro is set instead:

cmake_path(RELATIVE_PATH RE2C_STDLIB_DIR BASE_DIRECTORY "${CMAKE_INSTALL_PREFIX}/bin" OUTPUT_VARIABLE RE2C_STDLIB_RELATIVE_DIR)

This generates: ../share/re2c/stdlib.

Inside re2c, you can then append this relative path to the location of the running re2c binary to build an absolute path to the standard library.

This StackOverflow question suggests a few ways to compute the location of the re2c binary.

Related to this, a change of installation prefix triggers a complete rebuild.
Reproducer:

cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/tmp/a1
cmake --build build
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/tmp/a2
cmake --build build

Hi! I considered your suggested approach, and I see two problems with it:

  1. Detecting location of the re2c binary. As you can see from the answer to SO question, it is far from easy and very platform-dependent, so it would be hard to test (impossible on all platforms). The portable way argv[0] doesn't work because if re2c is in PATH, then argv[0] can be just re2c.

  2. A relative path works for the installed binary, but it makes the discovery of include files more opportunistic for the case of development binary (what if the relative include path just happens to be there and contains the wrong files?). Currently development binary won't find the include files unless you pass an explicit -I option, which is a safer default.

What is your use case? One possible way out is to create a wrapper script that would pass an explicit -I option after relocating re2c installation. I don't think other programs are easily relocatable; at least it is not the default expectation as far as I know.

bison accomplishes this by using gnulib. Others accomplish this by extra arguments (like your -I), or using environment variables.

My use case is adding re2c alongside a compiler toolchain for distribution.
I suppose a re2c wrapper script like you described will be the way the go.

I suppose a re2c wrapper script like you described will be the way the go.

Sounds good. Closing the bug, please reopen if the wrapper approach doesn't work.