bootstrap 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #!/bin/sh
  2. CMAKE_KNOWN_C_COMPILERS="cc gcc xlc icc tcc"
  3. CMAKE_KNOWN_CXX_COMPILERS="CC g++ c++ xlC icc como aCC"
  4. CMAKE_KNOWN_MAKE_PROCESSORS="make gmake"
  5. CMAKE_SOURCES="\
  6. cmake \
  7. cmakewizard \
  8. cmakemain \
  9. cmMakeDepend \
  10. cmMakefile \
  11. cmDocumentation \
  12. cmGlobalGenerator \
  13. cmLocalGenerator \
  14. cmRegularExpression \
  15. cmSourceFile \
  16. cmSystemTools \
  17. cmDirectory \
  18. cmGlobalUnixMakefileGenerator \
  19. cmLocalUnixMakefileGenerator \
  20. cmCommands \
  21. cmTarget \
  22. cmCustomCommand \
  23. cmCacheManager \
  24. cmListFileCache \
  25. cmVariableWatch \
  26. cmSourceGroup"
  27. cmake_system=`uname`
  28. cmake_source_dir=`echo $0 | sed -n '/\//{s/\/[^\/]*$//;p;}'`
  29. cmake_source_dir=`(cd "${cmake_source_dir}";pwd)`
  30. cmake_binary_dir=`pwd`
  31. cmake_bootstrap_dir="${cmake_binary_dir}/Bootstrap.cmk"
  32. # Display CMake bootstrap usage
  33. cmake_usage()
  34. {
  35. cat<<EOF
  36. Usage: $0 [options]
  37. Options: [defaults in brackets after descriptions]
  38. Configuration:
  39. --help print this message
  40. --version only print version information
  41. --verbose display more information
  42. --parallel=n bootstrap cmake in parallel, where n is
  43. number of nodes [1]
  44. Directory and file names:
  45. --prefix=PREFIX install architecture-independent files in PREFIX
  46. [/usr/local]
  47. EOF
  48. exit 10
  49. }
  50. # Display CMake bootstrap usage
  51. cmake_version()
  52. {
  53. # Get CMake version
  54. CMAKE_VERSION=""
  55. for a in MAJOR MINOR PATCH; do
  56. CMake_VERSION=`cat "${cmake_source_dir}/CMakeLists.txt" | grep "SET(CMake_VERSION_${a} *[0-9]*)" | sed "s/SET(CMake_VERSION_${a} *\([0-9]*\))/\1/"`
  57. CMAKE_VERSION="${CMAKE_VERSION}.${CMake_VERSION}"
  58. done
  59. CMAKE_VERSION=`echo $CMAKE_VERSION | sed "s/\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)/\1.\2-\3/"`
  60. echo "CMake ${CMAKE_VERSION}, Copyright (c) 2002 Kitware, Inc., Insight Consortium"
  61. }
  62. # Display CMake bootstrap error, display the log file and exit
  63. cmake_error()
  64. {
  65. echo "---------------------------------------------"
  66. echo "Error when bootstrapping CMake:"
  67. echo "$*"
  68. echo "---------------------------------------------"
  69. if [ -f cmake_bootstrap.log ]; then
  70. echo "Log of errors:"
  71. cat cmake_bootstrap.log
  72. echo "---------------------------------------------"
  73. fi
  74. exit 1
  75. }
  76. # Write string into a file
  77. cmake_report ()
  78. {
  79. FILE=$1
  80. shift
  81. echo "$*" >> ${FILE}
  82. }
  83. # Escape spaces in strings
  84. cmake_escape ()
  85. {
  86. echo $1 | sed "s/ /\\\\ /g"
  87. }
  88. # Write message to the log
  89. cmake_log ()
  90. {
  91. echo "$*" >> cmake_bootstrap.log
  92. }
  93. # Return temp file
  94. cmake_tmp_file ()
  95. {
  96. echo "cmake_bootstrap_$$.test"
  97. }
  98. # Run a compiler test. First argument is compiler, second one are compiler
  99. # flags, third one is test source file to be compiled
  100. cmake_try_run ()
  101. {
  102. COMPILER=$1
  103. FLAGS=$2
  104. TESTFILE=$3
  105. if [ ! -f "${TESTFILE}" ]; then
  106. echo "Test file ${TESTFILE} missing. Please verify your CMake source tree."
  107. exit 4
  108. fi
  109. TMPFILE=`cmake_tmp_file`
  110. echo "Try: ${COMPILER}"
  111. "${COMPILER}" ${FLAGS} "${TESTFILE}" -o "${TMPFILE}"
  112. RES=$?
  113. if [ "${RES}" -ne "0" ]; then
  114. echo "${COMPILER} does not work";return 1
  115. fi
  116. if [ ! -f "${TMPFILE}" ] && [ ! -f "${TMPFILE}.exe" ]; then
  117. echo "${COMPILER} does not produce output"
  118. return 2
  119. fi
  120. ./${TMPFILE}
  121. RES=$?
  122. rm -f "${TMPFILE}"
  123. if [ "${RES}" -ne "0" ]; then
  124. echo "${COMPILER} produces strange executable"
  125. return 3
  126. fi
  127. echo "${COMPILER} works"
  128. return 0
  129. }
  130. # Run a make test. First argument is the make interpreter.
  131. cmake_try_make ()
  132. {
  133. MAKE_PROC=$1
  134. echo "Try: ${MAKE_PROC}"
  135. ${MAKE_PROC}
  136. RES=$?
  137. if [ "${RES}" -ne "0" ]; then
  138. echo "${MAKE_PROC} does not work";return 1
  139. fi
  140. if [ ! -f "test" ] && [ ! -f "test.exe" ]; then
  141. echo "${COMPILER} does not produce output"
  142. return 2
  143. fi
  144. ./test
  145. RES=$?
  146. rm -f "test"
  147. if [ "${RES}" -ne "0" ]; then
  148. echo "${MAKE_PROC} produces strange executable"
  149. return 3
  150. fi
  151. echo "${MAKE_PROC} works"
  152. return 0
  153. }
  154. # Parse arguments
  155. cmake_verbose=
  156. cmake_parallel_make=
  157. cmake_prefix_dir="/usr/local"
  158. for a in "$@"; do
  159. if echo $a | grep "^--prefix=" > /dev/null 2> /dev/null; then
  160. cmake_prefix_dir=`echo $a | sed "s/^--prefix=//"`
  161. fi
  162. if echo $a | grep "^--parallel=" > /dev/null 2> /dev/null; then
  163. cmake_parallel_make=`echo $a | sed "s/^--parallel=//" | grep "[0-9][0-9]*"`
  164. fi
  165. if echo $a | grep "^--help" > /dev/null 2> /dev/null; then
  166. cmake_usage
  167. fi
  168. if echo $a | grep "^--version" > /dev/null 2> /dev/null; then
  169. cmake_version
  170. exit 2
  171. fi
  172. if echo $a | grep "^--verbose" > /dev/null 2> /dev/null; then
  173. cmake_verbose=TRUE
  174. fi
  175. done
  176. # If verbose, display some information about bootstrap
  177. if [ -n "${cmake_verbose}" ]; then
  178. echo "---------------------------------------------"
  179. echo "Source directory: ${cmake_source_dir}"
  180. echo "Binary directory: ${cmake_binary_dir}"
  181. echo "Prefix directory: ${cmake_prefix_dir}"
  182. echo "System: ${cmake_system}"
  183. if [ "x${cmake_parallel_make}" != "x" ]; then
  184. echo "Doing parallel make: ${cmake_parallel_make}"
  185. fi
  186. echo ""
  187. fi
  188. echo "---------------------------------------------"
  189. # Get CMake version
  190. echo "`cmake_version`"
  191. # Make bootstrap directory
  192. [ -d "${cmake_bootstrap_dir}" ] || mkdir "${cmake_bootstrap_dir}"
  193. if [ ! -d "${cmake_bootstrap_dir}" ]; then
  194. cmake_error "Cannot create directory ${cmake_bootstrap_dir} to bootstrap CMake."
  195. fi
  196. cd "${cmake_bootstrap_dir}"
  197. # Delete all the bootstrap files
  198. rm -f "${cmake_bootstrap_dir}/cmake_bootstrap.log"
  199. rm -f "${cmake_bootstrap_dir}/cmConfigure.h"
  200. # If exist compiler flags, set them
  201. cmake_c_flags=${CFLAGS}
  202. cmake_cxx_flags=${CXXFLAGS}
  203. # Test C compiler
  204. cmake_c_compiler=
  205. # If CC is set, use that for compiler, otherwise use list of known compilers
  206. if [ -n "${CC}" ]; then
  207. cmake_c_compilers="${CC}"
  208. else
  209. cmake_c_compilers="${CMAKE_KNOWN_C_COMPILERS}"
  210. fi
  211. # Check if C compiler works
  212. TMPFILE=`cmake_tmp_file`
  213. cat>"${TMPFILE}.c"<<EOF
  214. #include<stdio.h>
  215. int main()
  216. {
  217. printf("1\n");
  218. return 0;
  219. }
  220. EOF
  221. for a in ${cmake_c_compilers}; do
  222. if [ -z "${cmake_c_compiler}" ] && cmake_try_run "${a}" "${cmake_c_flags}" "${TMPFILE}.c" >> cmake_bootstrap.log 2>&1; then
  223. cmake_c_compiler="${a}"
  224. fi
  225. done
  226. rm -f "${TMPFILE}.c"
  227. if [ -z "${cmake_c_compiler}" ]; then
  228. cmake_error "Cannot find apropriate C compiler on this system.
  229. Please specify one using environment variable CC."
  230. fi
  231. echo "C compiler on this system is: ${cmake_c_compiler} ${cmake_c_flags}"
  232. # Test CXX compiler
  233. cmake_cxx_compiler=
  234. # If CC is set, use that for compiler, otherwise use list of known compilers
  235. if [ -n "${CXX}" ]; then
  236. cmake_cxx_compilers="${CXX}"
  237. else
  238. cmake_cxx_compilers="${CMAKE_KNOWN_CXX_COMPILERS}"
  239. fi
  240. # Check if C++ compiler works
  241. TMPFILE=`cmake_tmp_file`
  242. cat>"${TMPFILE}.cxx"<<EOF
  243. #include <stdio.h>
  244. class NeedCXX {};
  245. int main()
  246. {
  247. printf("1\n");
  248. return 0;
  249. }
  250. EOF
  251. for a in ${cmake_cxx_compilers}; do
  252. if [ -z "${cmake_cxx_compiler}" ] && cmake_try_run "${a}" "${cmake_cxx_flags}" "${TMPFILE}.cxx" >> cmake_bootstrap.log 2>&1; then
  253. cmake_cxx_compiler="${a}"
  254. fi
  255. done
  256. rm -f "${TMPFILE}.cxx"
  257. if [ -z "${cmake_cxx_compiler}" ]; then
  258. cmake_error "Cannot find apropriate C++ compiler on this system.
  259. Please specify one using environment variable CXX."
  260. fi
  261. echo "C++ compiler on this system is: ${cmake_cxx_compiler} ${cmake_cxx_flags}"
  262. # Test Make
  263. cmake_make_processor=
  264. # If MAKE is set, use that for make processor, otherwise use list of known make
  265. if [ -n "${MAKE}" ]; then
  266. cmake_make_processors="${MAKE}"
  267. else
  268. cmake_make_processors="${CMAKE_KNOWN_MAKE_PROCESSORS}"
  269. fi
  270. TMPFILE="`cmake_tmp_file`_dir"
  271. rm -rf "${cmake_bootstrap_dir}/${TMPFILE}"
  272. mkdir "${cmake_bootstrap_dir}/${TMPFILE}"
  273. cd "${cmake_bootstrap_dir}/${TMPFILE}"
  274. cat>"Makefile"<<EOF
  275. test: test.c
  276. ${cmake_c_compiler} -o test test.c
  277. EOF
  278. cat>"test.c"<<EOF
  279. #include <stdio.h>
  280. int main(){ printf("1\n"); return 0; }
  281. EOF
  282. for a in ${cmake_make_processors}; do
  283. if [ -z "${cmake_make_processor}" ] && cmake_try_make "${a}" >> cmake_bootstrap.log 2>&1; then
  284. cmake_make_processor="${a}"
  285. fi
  286. done
  287. cd "${cmake_bootstrap_dir}"
  288. rm -rf "${cmake_bootstrap_dir}/${TMPFILE}"
  289. echo "Make processor on this system is: ${cmake_make_processor}"
  290. # Ok, we have CC, CXX, and MAKE.
  291. # Test C++ compiler features
  292. # If we are on IRIX, check for -LANG:std
  293. if [ "x${cmake_system}" = "xIRIX64" ]; then
  294. TMPFILE=`cmake_tmp_file`
  295. cat>${TMPFILE}.cxx<<EOF
  296. #include <iostream>
  297. int main() { std::cout << "No need for -LANG:std" << std::endl; return 0;}
  298. EOF
  299. cmake_need_lang_std=0
  300. if cmake_try_run "${cmake_cxx_compiler}" "${cmake_cxx_flags}" "${TMPFILE}.cxx" >> cmake_bootstrap.log 2>&1; then
  301. :
  302. else
  303. if cmake_try_run "${cmake_cxx_compiler}" "${cmake_cxx_flags} -LANG:std" "${TMPFILE}.cxx" >> cmake_bootstrap.log 2>&1; then
  304. cmake_need_lang_std=1
  305. fi
  306. fi
  307. if [ "x${cmake_need_lang_std}" = "x1" ]; then
  308. cmake_cxx_flags="${cmake_cxx_flags} -LANG:std"
  309. echo "${cmake_cxx_compiler} needs -LANG:std"
  310. else
  311. echo "${cmake_cxx_compiler} does not need -LANG:std"
  312. fi
  313. rm -f "${TMPFILE}.cxx"
  314. fi
  315. # Test for STD namespace
  316. if cmake_try_run "${cmake_cxx_compiler}" "${cmake_cxx_flags}" "${cmake_source_dir}/Modules/TestForSTDNamespace.cxx" >> cmake_bootstrap.log 2>&1; then
  317. cmake_report cmConfigure.h "/* #undef CMAKE_NO_STD_NAMESPACE */"
  318. echo "${cmake_cxx_compiler} has STD namespace"
  319. else
  320. cmake_report cmConfigure.h "#define CMAKE_NO_STD_NAMESPACE 1"
  321. echo "${cmake_cxx_compiler} does not have STD namespace"
  322. fi
  323. # Test for ANSI stream headers
  324. if cmake_try_run "${cmake_cxx_compiler}" "${cmake_cxx_flags}" "${cmake_source_dir}/Modules/TestForANSIStreamHeaders.cxx" >> cmake_bootstrap.log 2>&1; then
  325. cmake_report cmConfigure.h "/* #undef CMAKE_NO_ANSI_STREAM_HEADERS */"
  326. echo "${cmake_cxx_compiler} has ANSI stream headers"
  327. else
  328. cmake_report cmConfigure.h "#define CMAKE_NO_ANSI_STREAM_HEADERS 1"
  329. echo "${cmake_cxx_compiler} does not have ANSI stream headers"
  330. fi
  331. # Test for ansi string streams
  332. TMPFILE=`cmake_tmp_file`
  333. cat>${TMPFILE}.cxx<<EOF
  334. #include <sstream>
  335. int main() { return 0;}
  336. EOF
  337. if cmake_try_run "${cmake_cxx_compiler}" "${cmake_cxx_flags}" "${TMPFILE}.cxx" >> cmake_bootstrap.log 2>&1; then
  338. cmake_report cmConfigure.h "/* #undef CMAKE_NO_ANSI_STRING_STREAM */"
  339. echo "${cmake_cxx_compiler} has ANSI string streams"
  340. else
  341. cmake_report cmConfigure.h "#define CMAKE_NO_ANSI_STRING_STREAM 1"
  342. echo "${cmake_cxx_compiler} does not have ANSI string streams"
  343. fi
  344. rm -f "${TMPFILE}.cxx"
  345. # Test for ansi FOR scope
  346. if cmake_try_run "${cmake_cxx_compiler}" "${cmake_cxx_flags}" "${cmake_source_dir}/Modules/TestForAnsiForScope.cxx" >> cmake_bootstrap.log 2>&1; then
  347. cmake_report cmConfigure.h "/* #undef CMAKE_NO_ANSI_FOR_SCOPE */"
  348. echo "${cmake_cxx_compiler} has ANSI for scoping"
  349. else
  350. cmake_report cmConfigure.h "#define CMAKE_NO_ANSI_FOR_SCOPE 1"
  351. echo "${cmake_cxx_compiler} does not have ANSI for scoping"
  352. fi
  353. # Write CMake version
  354. for a in MAJOR MINOR PATCH; do
  355. CMake_VERSION=`cat "${cmake_source_dir}/CMakeLists.txt" | grep "SET(CMake_VERSION_${a} *[0-9]*)" | sed "s/SET(CMake_VERSION_${a} *\([0-9]*\))/\1/"`
  356. cmake_report cmConfigure.h "#define CMake_VERSION_${a} ${CMake_VERSION}"
  357. done
  358. cmake_report cmConfigure.h "#define CMAKE_ROOT_DIR \"${cmake_source_dir}\""
  359. cmake_report cmConfigure.h "#define CMAKE_BOOTSTRAP"
  360. # Generate Makefile
  361. dep="cmConfigure.h `cmake_escape \"${cmake_source_dir}\"`/Source/*.h"
  362. objs=""
  363. for a in ${CMAKE_SOURCES}; do
  364. objs="${objs} ${a}.o"
  365. done
  366. cmake_cxx_flags="${cmake_ansi_cxx_flags} ${cmake_cxx_flags} -I`cmake_escape \"${cmake_source_dir}/Source\"` -I`cmake_escape \"${cmake_bootstrap_dir}\"`"
  367. echo "cmake: ${objs}" > "${cmake_bootstrap_dir}/Makefile"
  368. echo " ${cmake_cxx_compiler} ${LDFLAGS} ${cmake_cxx_flags} ${objs} -o cmake" >> "${cmake_bootstrap_dir}/Makefile"
  369. for a in ${CMAKE_SOURCES}; do
  370. src=`cmake_escape "${cmake_source_dir}/Source/${a}.cxx"`
  371. echo "${a}.o : ${src} ${dep}" >> "${cmake_bootstrap_dir}/Makefile"
  372. echo " ${cmake_cxx_compiler} ${cmake_cxx_flags} -c ${src} -o ${a}.o" >> "${cmake_bootstrap_dir}/Makefile"
  373. done
  374. # Write prefix to Bootstrap.cmk/InitialConfigureFlags.cmake
  375. echo "SET (CMAKE_CONFIGURE_INSTALL_PREFIX \"${cmake_prefix_dir}\" CACHE PATH \"Install path prefix, prepended onto install directories, For CMake this will always override CMAKE_INSTALL_PREFIX in the cache.\")" > "${cmake_bootstrap_dir}/InitialConfigureFlags.cmake"
  376. echo "---------------------------------------------"
  377. # Run make to build bootstrap cmake
  378. if [ "x${cmake_parallel_make}" != "x" ]; then
  379. ${cmake_make_processor} -j ${cmake_parallel_make}
  380. else
  381. ${cmake_make_processor}
  382. fi
  383. RES=$?
  384. if [ "${RES}" -ne "0" ]; then
  385. cmake_error "Problem while bootstrapping CMake"
  386. fi
  387. cd "${cmake_binary_dir}"
  388. # Set C, CXX, and MAKE environment variables, so that real real cmake will be
  389. # build with same compiler and make
  390. CC="${cmake_c_compiler}"
  391. CXX="${cmake_cxx_compiler}"
  392. MAKE="${cmake_make_processor}"
  393. export CC
  394. export CXX
  395. export MAKE
  396. # Run bootstrap CMake to configure real CMake
  397. "${cmake_bootstrap_dir}/cmake" "${cmake_source_dir}"
  398. echo "---------------------------------------------"
  399. # And we are done. Now just run make
  400. echo "CMake ${CMAKE_VERSION} is configured. Now just run ${cmake_make_processor}."