Skip to content

Commit 408dbc5

Browse files
committed
[C] Download and install CMake during build.
1 parent b702404 commit 408dbc5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ cppbuild/*.cache
6464
cppbuild/Debug
6565
cppbuild/Release
6666
cppbuild/Win32
67+
cppbuild/cmake
6768

6869
# golang
6970
gocode/pkg

cppbuild/cppbuild

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,44 @@ SOURCE_DIR="$(pwd)"
44
BUILD_DIR="${SOURCE_DIR}/cppbuild/Release"
55
EXTRA_CMAKE_ARGS=""
66

7+
BUILD_DELETE_CMAKE=false
8+
BUILD_CMAKE_VERSION=4.2.1
9+
BUILD_CMAKE_DIR="${SOURCE_DIR}/cppbuild/cmake"
10+
711
ncpus=1
812
case "$(uname)" in
913
Darwin* )
1014
ncpus=$(sysctl -n hw.ncpu)
15+
BUILD_CMAKE_OS="macos-universal"
16+
BUILD_CMAKE_PATH="${BUILD_CMAKE_DIR}/CMake.app/Contents/bin"
1117
;;
1218
Linux*)
1319
ncpus=$(lscpu -p | grep -c -E -v '^#')
20+
BUILD_CMAKE_OS="linux-$(arch)"
21+
BUILD_CMAKE_PATH="${BUILD_CMAKE_DIR}/bin"
1422
;;
1523
esac
1624

25+
function install_cmake()
26+
{
27+
if [[ true = "${BUILD_DELETE_CMAKE}" ]]
28+
then
29+
echo "Removing old CMake installation"
30+
rm -rf "${BUILD_CMAKE_DIR}"
31+
fi
32+
33+
local version_file="${BUILD_CMAKE_DIR}/version.txt"
34+
local version_info="${BUILD_CMAKE_VERSION}-${BUILD_CMAKE_OS}"
35+
if [[ "${version_info}" != "$(cat "${version_file}")" ]]
36+
then
37+
echo "Installing CMake ${version_info}"
38+
rm -rf "${BUILD_CMAKE_DIR}"
39+
mkdir -p "${BUILD_CMAKE_DIR}"
40+
(curl -LJ "https://github.com/Kitware/CMake/releases/download/v${BUILD_CMAKE_VERSION}/cmake-${BUILD_CMAKE_VERSION}-${BUILD_CMAKE_OS}.tar.gz" | tar xzf - -C "${BUILD_CMAKE_DIR}" --strip-components 1
41+
echo "${version_info}" > "${version_file}")
42+
fi
43+
}
44+
1745
while [[ $# -gt 0 ]]
1846
do
1947
option="${1}"
@@ -39,6 +67,16 @@ do
3967
echo "Enabling sanitise build"
4068
shift
4169
;;
70+
--rebuild-cmake)
71+
BUILD_DELETE_CMAKE=true
72+
shift
73+
;;
74+
--cmake-version)
75+
BUILD_CMAKE_VERSION=${2}
76+
echo "Setting BUILD_CMAKE_VERSION=${2}"
77+
shift
78+
shift
79+
;;
4280
-h|--help)
4381
echo "${0} [--c-warnings-as-errors] [--cxx-warnings-as-errors] [--debug-build] [--sanitise-build]"
4482
exit
@@ -59,6 +97,9 @@ if [ -d "${BUILD_DIR}" ] ; then
5997
fi
6098

6199
mkdir -p "${BUILD_DIR}"
100+
101+
install_cmake
102+
62103
cd "${BUILD_DIR}" || exit
63104

64-
(cmake -G "Unix Makefiles" ${EXTRA_CMAKE_ARGS} "${SOURCE_DIR}" && make clean && make -j ${ncpus} all && ctest -C Release --output-on-failure)
105+
("${BUILD_CMAKE_PATH}/cmake" -G "Unix Makefiles" ${EXTRA_CMAKE_ARGS} "${SOURCE_DIR}" && make clean && make -j ${ncpus} all && "${BUILD_CMAKE_PATH}/ctest" -C Release --output-on-failure)

0 commit comments

Comments
 (0)