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