Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / by-name / cm / cmake / setup-hook.sh
blobb28ed42b6896b227365cde1a087cca8984b73904
1 addCMakeParams() {
2 addToSearchPath CMAKE_PREFIX_PATH $1
5 fixCmakeFiles() {
6 # Replace occurences of /usr and /opt by /var/empty.
7 echo "fixing cmake files..."
8 find "$1" \( -type f -name "*.cmake" -o -name "*.cmake.in" -o -name CMakeLists.txt \) -print |
9 while read fn; do
10 sed -e 's^/usr\([ /]\|$\)^/var/empty\1^g' -e 's^/opt\([ /]\|$\)^/var/empty\1^g' < "$fn" > "$fn.tmp"
11 mv "$fn.tmp" "$fn"
12 done
15 cmakeConfigurePhase() {
16 runHook preConfigure
18 # default to CMake defaults if unset
19 : ${cmakeBuildDir:=build}
21 export CTEST_OUTPUT_ON_FAILURE=1
22 if [ -n "${enableParallelChecking-1}" ]; then
23 export CTEST_PARALLEL_LEVEL=$NIX_BUILD_CORES
26 if [ -z "${dontFixCmake-}" ]; then
27 fixCmakeFiles .
30 if [ -z "${dontUseCmakeBuildDir-}" ]; then
31 mkdir -p "$cmakeBuildDir"
32 cd "$cmakeBuildDir"
33 : ${cmakeDir:=..}
34 else
35 : ${cmakeDir:=.}
38 if [ -z "${dontAddPrefix-}" ]; then
39 cmakeFlags="-DCMAKE_INSTALL_PREFIX=$prefix $cmakeFlags"
42 # We should set the proper `CMAKE_SYSTEM_NAME`.
43 # http://www.cmake.org/Wiki/CMake_Cross_Compiling
45 # Unfortunately cmake seems to expect absolute paths for ar, ranlib, and
46 # strip. Otherwise they are taken to be relative to the source root of the
47 # package being built.
48 cmakeFlags="-DCMAKE_CXX_COMPILER=$CXX $cmakeFlags"
49 cmakeFlags="-DCMAKE_C_COMPILER=$CC $cmakeFlags"
50 cmakeFlags="-DCMAKE_AR=$(command -v $AR) $cmakeFlags"
51 cmakeFlags="-DCMAKE_RANLIB=$(command -v $RANLIB) $cmakeFlags"
52 cmakeFlags="-DCMAKE_STRIP=$(command -v $STRIP) $cmakeFlags"
54 # on macOS we want to prefer Unix-style headers to Frameworks
55 # because we usually do not package the framework
56 cmakeFlags="-DCMAKE_FIND_FRAMEWORK=LAST $cmakeFlags"
58 # we never want to use the global macOS SDK
59 cmakeFlags="-DCMAKE_OSX_SYSROOT= $cmakeFlags"
61 # correctly detect our clang compiler
62 cmakeFlags="-DCMAKE_POLICY_DEFAULT_CMP0025=NEW $cmakeFlags"
64 # This installs shared libraries with a fully-specified install
65 # name. By default, cmake installs shared libraries with just the
66 # basename as the install name, which means that, on Darwin, they
67 # can only be found by an executable at runtime if the shared
68 # libraries are in a system path or in the same directory as the
69 # executable. This flag makes the shared library accessible from its
70 # nix/store directory.
71 cmakeFlags="-DCMAKE_INSTALL_NAME_DIR=${!outputLib}/lib $cmakeFlags"
73 # The docdir flag needs to include PROJECT_NAME as per GNU guidelines,
74 # try to extract it from CMakeLists.txt.
75 if [[ -z "$shareDocName" ]]; then
76 local cmakeLists="${cmakeDir}/CMakeLists.txt"
77 if [[ -f "$cmakeLists" ]]; then
78 local shareDocName="$(grep --only-matching --perl-regexp --ignore-case '\bproject\s*\(\s*"?\K([^[:space:]")]+)' < "$cmakeLists" | head -n1)"
80 # The argument sometimes contains garbage or variable interpolation.
81 # When that is the case, let’s fall back to the derivation name.
82 if [[ -z "$shareDocName" ]] || echo "$shareDocName" | grep -q '[^a-zA-Z0-9_+-]'; then
83 if [[ -n "${pname-}" ]]; then
84 shareDocName="$pname"
85 else
86 shareDocName="$(echo "$name" | sed 's/-[^a-zA-Z].*//')"
91 # This ensures correct paths with multiple output derivations
92 # It requires the project to use variables from GNUInstallDirs module
93 # https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
94 cmakeFlags="-DCMAKE_INSTALL_BINDIR=${!outputBin}/bin $cmakeFlags"
95 cmakeFlags="-DCMAKE_INSTALL_SBINDIR=${!outputBin}/sbin $cmakeFlags"
96 cmakeFlags="-DCMAKE_INSTALL_INCLUDEDIR=${!outputInclude}/include $cmakeFlags"
97 cmakeFlags="-DCMAKE_INSTALL_OLDINCLUDEDIR=${!outputInclude}/include $cmakeFlags"
98 cmakeFlags="-DCMAKE_INSTALL_MANDIR=${!outputMan}/share/man $cmakeFlags"
99 cmakeFlags="-DCMAKE_INSTALL_INFODIR=${!outputInfo}/share/info $cmakeFlags"
100 cmakeFlags="-DCMAKE_INSTALL_DOCDIR=${!outputDoc}/share/doc/${shareDocName} $cmakeFlags"
101 cmakeFlags="-DCMAKE_INSTALL_LIBDIR=${!outputLib}/lib $cmakeFlags"
102 cmakeFlags="-DCMAKE_INSTALL_LIBEXECDIR=${!outputLib}/libexec $cmakeFlags"
103 cmakeFlags="-DCMAKE_INSTALL_LOCALEDIR=${!outputLib}/share/locale $cmakeFlags"
105 # Don’t build tests when doCheck = false
106 if [ -z "${doCheck-}" ]; then
107 cmakeFlags="-DBUILD_TESTING=OFF $cmakeFlags"
110 # Always build Release, to ensure optimisation flags
111 cmakeFlags="-DCMAKE_BUILD_TYPE=${cmakeBuildType:-Release} $cmakeFlags"
113 # Disable user package registry to avoid potential side effects
114 # and unecessary attempts to access non-existent home folder
115 # https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#disabling-the-package-registry
116 cmakeFlags="-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON $cmakeFlags"
117 cmakeFlags="-DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF $cmakeFlags"
118 cmakeFlags="-DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF $cmakeFlags"
120 if [ "${buildPhase-}" = ninjaBuildPhase ]; then
121 cmakeFlags="-GNinja $cmakeFlags"
124 echo "cmake flags: $cmakeFlags ${cmakeFlagsArray[@]}"
126 cmake "$cmakeDir" $cmakeFlags "${cmakeFlagsArray[@]}"
128 if ! [[ -v enableParallelBuilding ]]; then
129 enableParallelBuilding=1
130 echo "cmake: enabled parallel building"
133 if ! [[ -v enableParallelInstalling ]]; then
134 enableParallelInstalling=1
135 echo "cmake: enabled parallel installing"
138 runHook postConfigure
141 if [ -z "${dontUseCmakeConfigure-}" -a -z "${configurePhase-}" ]; then
142 setOutputFlags=
143 configurePhase=cmakeConfigurePhase
146 addEnvHooks "$targetOffset" addCMakeParams
148 makeCmakeFindLibs(){
149 isystem_seen=
150 iframework_seen=
151 for flag in ${NIX_CFLAGS_COMPILE-} ${NIX_LDFLAGS-}; do
152 if test -n "$isystem_seen" && test -d "$flag"; then
153 isystem_seen=
154 export CMAKE_INCLUDE_PATH="${CMAKE_INCLUDE_PATH-}${CMAKE_INCLUDE_PATH:+:}${flag}"
155 elif test -n "$iframework_seen" && test -d "$flag"; then
156 iframework_seen=
157 export CMAKE_FRAMEWORK_PATH="${CMAKE_FRAMEWORK_PATH-}${CMAKE_FRAMEWORK_PATH:+:}${flag}"
158 else
159 isystem_seen=
160 iframework_seen=
161 case $flag in
162 -I*)
163 export CMAKE_INCLUDE_PATH="${CMAKE_INCLUDE_PATH-}${CMAKE_INCLUDE_PATH:+:}${flag:2}"
165 -L*)
166 export CMAKE_LIBRARY_PATH="${CMAKE_LIBRARY_PATH-}${CMAKE_LIBRARY_PATH:+:}${flag:2}"
168 -F*)
169 export CMAKE_FRAMEWORK_PATH="${CMAKE_FRAMEWORK_PATH-}${CMAKE_FRAMEWORK_PATH:+:}${flag:2}"
171 -isystem)
172 isystem_seen=1
174 -iframework)
175 iframework_seen=1
177 esac
179 done
182 # not using setupHook, because it could be a setupHook adding additional
183 # include flags to NIX_CFLAGS_COMPILE
184 postHooks+=(makeCmakeFindLibs)