8000 Relocatable SWIG: allow locating SwigLib relative to executable in CMakeLists.txt by jmarrec · Pull Request #2509 · swig/swig · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Relocatable SWIG: allow locating SwigLib relative to executable in CMakeLists.txt #2509

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

jmarrec
Copy link
Contributor
@jmarrec jmarrec commented Mar 14, 2023

The idea is:

  • This is opt-in, leaving the historical way untouched
  • Allow customizing the location of that directory
    • including on windows (where it was already relative).
    • On unix, via a dladdr

This is motivated by being able to use swig via conan, where the packages are by definition relocated. We had patched it on the conan recipe's side, but now that CMakeLists.txt has been added, I think there's a good case for cleaning and upstreaming the changes, so here it is. (I am terrible at autotools to be honest, so that was one of the roadblocks)

Example:

SWIG_LIB_RELATIVE_TO_EXE is only needed here, but I'm also choosing to override it's install PATH via SWIG_LIB_RELATIVE_PATH which would default to <install_prefix>/Lib otherwise

cd swig && cd .. && mkdir swig-build && cd swig-build
cmake -G Ninja -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_INSTALL_PREFIX:PATH=$(pwd)/install \
      -DSWIG_LIB_RELATIVE_TO_EXE:BOOL=ON \
      -DSWIG_LIB_RELATIVE_PATH:STRING=bin/swiglib ../swig
ninja install
$ ls ./install/bin/
swig  swiglib

$ ./install/bin/swig -swiglib
/home/julien/Software/Others/swig-build/install/bin/swiglib

Relocate:

$ mv install relocated_install
$ ./relocated_install/bin/swig -swiglib
/home/julien/Software/Others/swig-build/relocated_install/bin/swiglib

@leleliu008
Copy link
leleliu008 commented May 14, 2023

if dladdr is used, swig can be built as fully statically linked executable? if not, I think we should use other way to locate current running exec location.

@jmarrec jmarrec force-pushed the swig_lib_relative branch from fc5f8aa to bb130ad Compare June 16, 2023 09:53
@wsfulton
Copy link
Member

If this pull request is still needed, it'll need to pass the tests for serious consideration. They are currently failing due to the changes in here.

@jmarrec jmarrec force-pushed the swig_lib_relative branch from bb130ad to 60f016a Compare June 19, 2023 12:22
@jmarrec
Copy link
Contributor Author
jmarrec commented Jun 19, 2023

If this pull request is still needed, it'll need to pass the tests for serious consideration. They are currently failing due to the changes in here.

Corrected a typo. The nuget build on GHA is passing now. Appveyor should report in 5 hours.

And yes, I'd like to kindly ask for serious consideration here.

if (SWIG_LIB_RELATIVE_TO_EXE)
target_link_libraries(swig ${CMAKE_DL_LIBS})
endif()
install (TARGETS swig DESTINATION bin) # TODO: seems like the executable in the zipped swigwin is actually installed at the root...
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • TODO: what's the actual behavior that's wanted here? Install swig(.exe) in bin/ on all platforms? Or root on windows?

# endif
static String *get_swig_lib_path(void) {
char buf[MAX_PATH];
// TODO: if this is actually in install/bin/, need another call to strrchr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • TODO: based on the answer to whether swig.exe is in bin/ when insalled or not, 60f016a should be kept (if in bin/) or reverted (if at root)

Comment on lines +830 to +815
# ifndef SWIG_LIB_RELATIVE_PATH
# define SWIG_LIB_RELATIVE_PATH "Lib"
# endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback when autotools on windows.

@LecrisUT
Copy link
LecrisUT commented Sep 7, 2023

What is the issue on conan side that CMAKE_INSTALL_PREFIX is not viable?

I think a more long-term solution would be to define a swig.conf that is loaded and overloaded as $PREFIX/etc/swig.conf -> $HOME/.local/conf/swig.conf -> $(pwd)/swig.conf, and then the paths for the libs can be defined in the conf file. CLI11 offers the back-end to load the configuration with cli and environment variable overloads (just the preference of conf file is missing I think).

@jmarrec
Copy link
Contributor Author
jmarrec commented Sep 7, 2023

What is the issue on conan side that CMAKE_INSTALL_PREFIX is not viable?

I think a more long-term solution would be to define a swig.conf that is loaded and overloaded as $PREFIX/etc/swig.conf -> $HOME/.local/conf/swig.conf -> $(pwd)/swig.conf, and then the paths for the libs can be defined in the conf file. CLI11 offers the back-end to load the configuration with cli and environment variable overloads (just the preference of conf file is missing I think).

As I noted in the OP:

This is motivated by being able to use swig via conan, where the packages are by definition relocated.

Conan packages are moved around.

Even if you build the package locally, locally you build in /path/to/build -> install in there -> move to /path/to/package

Generally the package binaries are built on another machine completely and placed into your /path/to/package path.

So you can't harcode the swig lib path.

@LecrisUT
Copy link
LecrisUT commented Sep 7, 2023

Ok, but isn't the end-goal using conan to install to the production path after the dependency paths are pulled? I.e. for Debug build, use SWIG_LIB environment variable.

I plan to design the bundled USESwig.cmake module in a way that if the project is imported from find_package(Swig), do not set SWIG_LIB, but otherwise set SWIG_LIB to binarydir. But for that I want to confirm one thing. Is conan + cmake doing a cmake --install step and other projects are then using them via find_package(), or can it be used like with FetchContent?

@dg0yt
Copy link
dg0yt commented Oct 12, 2024

This would be useful for any package manager with binary caching and variable installation locations. (Coming here from reviewing a vcpkg PR.)

@wsfulton
Copy link
Member

The autotools are the primary build system and CMake is a secondary build system. While I'm happy to accept CMake patches and do my best to review and test them, this option needs to be available using autotools too.

The approach of using a location set at build time has worked on Unix systems since the nineties. Many Unix executables require some env variables or a configuration installed, usually into /etc. I don't know vcpkg nor conan, do they not work like all the other unix tools installed the OS package managers? If not what is the generally accepted approach? SWIG is not the first Unix executable to require a configuration of some sort, so this must be a solved problem.

@leleliu008
8000 Copy link
mv swig swig.exe

cat > swig <<EOF
#!/bin/sh
cd \`dirname \$0\`
export SWIG_LIB="\$PWD/../share/swig/4.1.1"
exec ./swig.exe "\$@"

chmod +x swig

@ojwb
Copy link
Member
ojwb commented Apr 14, 2025

It seems there's a lack of progress here, and I see conan has dropped use of this (conan-io/conan-center-index#26734) so I wonder if this should just be closed.

The patch also uses dladdr(), which was only added to POSIX less than a year ago so we presumably can't rely on it.

Using a wrapper script which sets SWIG_LIB based on $0 as suggested in the previous comment seems a decent solution for packagers who want to support relocatable installations.

@ojwb
Copy link
Member
ojwb commented May 18, 2025

No disagreement in over a month so closing.

@ojwb ojwb closed this May 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0