CMake: defines and uses macro add_llvm_definitions for keeping track
[opencl/llvm.git] / tools / llvm-config / CMakeLists.txt
1 include(TestBigEndian)
3 if( NOT PERL_FOUND )
4   message(FATAL_ERROR "Perl required but not found!")
5 endif( NOT PERL_FOUND )
7 set(PERL ${PERL_EXECUTABLE})
8 set(VERSION PACKAGE_VERSION)
9 set(PREFIX ${LLVM_BINARY_DIR}) # TODO: Root for `make install'.
10 execute_process(COMMAND date
11   OUTPUT_VARIABLE LLVM_CONFIGTIME
12   OUTPUT_STRIP_TRAILING_WHITESPACE)
13 # LLVM_ON_UNIX and LLVM_ON_WIN32 already set.
14 # those are set to blank by `autoconf' on MinGW, so it seems they are not required:
15 #set(LLVMGCCDIR "")
16 #set(LLVMGCC "")
17 #set(LLVMGXX "")
18 #set(LLVMGCC_VERSION "")
19 #set(LLVMGCC_MAJVERS "")
20 test_big_endian(IS_BIG_ENDIAN)
21 if( IS_BIG_ENDIAN )
22   set(ENDIAN "big")
23 else( IS_BIG_ENDIAN )
24   set(ENDIAN "little")
25 endif( IS_BIG_ENDIAN )
26 set(SHLIBEXT ${LTDL_SHLIB_EXT})
27 #EXEEXT already set.
28 set(OS "${CMAKE_SYSTEM}")
29 set(ARCH "X86") # TODO: This gives "i686" in Linux: "${CMAKE_SYSTEM_PROCESSOR}")
31 include(GetTargetTriple)
32 get_target_triple(target)
34 foreach(c ${LLVM_TARGETS_TO_BUILD})
35   set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
36 endforeach(c)
37 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
38 set(TARGET_HAS_JIT "1")  # TODO
40 # Avoids replacement at config-time:
41 set(LLVM_CPPFLAGS "@LLVM_CPPFLAGS@")
42 set(LLVM_CFLAGS "@LLVM_CFLAGS@")
43 set(LLVM_CXXFLAGS "@LLVM_CXXFLAGS@")
44 set(LLVM_LDFLAGS "@LLVM_LDFLAGS@")
45 set(LIBS "@LIBS@")
46 set(LLVM_BUILDMODE "@LLVM_BUILDMODE@")
48 configure_file(
49   ${CMAKE_CURRENT_SOURCE_DIR}/llvm-config.in.in
50   ${CMAKE_CURRENT_BINARY_DIR}/llvm-config.in
51   @ONLY
52 )
54 set(LIBDEPS ${CMAKE_CURRENT_BINARY_DIR}/LibDeps.txt)
55 set(LIBDEPS_TMP ${CMAKE_CURRENT_BINARY_DIR}/LibDeps.txt.tmp)
56 set(FINAL_LIBDEPS ${CMAKE_CURRENT_BINARY_DIR}/FinalLibDeps.txt)
57 set(LLVM_CONFIG ${LLVM_TOOLS_BINARY_DIR}/llvm-config)
58 set(LLVM_CONFIG_IN ${CMAKE_CURRENT_BINARY_DIR}/llvm-config.in)
60 if( CMAKE_CROSSCOMPILING )
61   set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
62 endif()
64 find_program(NM_PATH nm PATH_SUFFIXES /bin)
66 if( NOT NM_PATH )
67   message(FATAL_ERROR "`nm' not found")
68 endif()
70 add_custom_command(OUTPUT ${LIBDEPS_TMP}
71   COMMAND ${PERL_EXECUTABLE} ${LLVM_MAIN_SRC_DIR}/utils/GenLibDeps.pl -flat ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR} ${NM_PATH} > ${LIBDEPS_TMP}
72   DEPENDS ${llvm_libs}
73   COMMENT "Regenerating ${LIBDEPS_TMP}")
75 add_custom_command(OUTPUT ${LIBDEPS}
76   COMMAND ${CMAKE_COMMAND} -E copy_if_different ${LIBDEPS_TMP} ${LIBDEPS}
77   DEPENDS ${LIBDEPS_TMP}
78   COMMENT "Updated ${LIBDEPS} because dependencies changed")
80 add_custom_command(OUTPUT ${FINAL_LIBDEPS}
81   COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/find-cycles.pl < ${LIBDEPS} > ${FINAL_LIBDEPS} || ${CMAKE_COMMAND} -E remove -f ${FINAL_LIBDEPS}
82   DEPENDS ${LIBDEPS}
83   COMMENT "Checking for cyclic dependencies between LLVM libraries.")
85 string(TOUPPER ${CMAKE_BUILD_TYPE} uppercase_CMAKE_BUILD_TYPE)
86 set(C_FLGS "${CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
87 set(CXX_FLGS "${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
88 set(CPP_FLGS "${CMAKE_CPP_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${LLVM_DEFINITIONS}")
90 add_custom_command(OUTPUT ${LLVM_CONFIG}
91   COMMAND echo 's!@LLVM_CPPFLAGS@!${CPP_FLGS}!' > temp.sed
92   COMMAND echo 's!@LLVM_CFLAGS@!${C_FLGS}!' >> temp.sed
93   COMMAND echo 's!@LLVM_CXXFLAGS@!${CXX_FLGS}!' >> temp.sed
94   # TODO: Use general flags for linking! not just for shared libs:
95   COMMAND echo 's!@LLVM_LDFLAGS@!${CMAKE_SHARED_LINKER_FLAGS}!' >> temp.sed
96   COMMAND echo 's!@LIBS@!!' >> temp.sed                    # TODO: System libs
97   COMMAND echo 's!@LLVM_BUILDMODE@!${CMAKE_BUILD_TYPE}!' >> temp.sed
98   COMMAND sed -f temp.sed < ${LLVM_CONFIG_IN} > ${LLVM_CONFIG}
99   COMMAND ${CMAKE_COMMAND} -E remove -f temp.sed
100   COMMAND cat ${FINAL_LIBDEPS} >> ${LLVM_CONFIG}
101   COMMAND chmod +x ${LLVM_CONFIG}
102   COMMAND cd ${CMAKE_BINARY_DIR} && ${CMAKE_COMMAND} -U HAVE_LLVM_CONFIG -D LLVM_BINARY_DIR="${LLVM_BINARY_DIR}" ${CMAKE_SOURCE_DIR}
103   DEPENDS ${FINAL_LIBDEPS} ${LLVM_CONFIG_IN}
104   COMMENT "Building llvm-config script."
105   )
107 add_custom_target(llvm-config.target ALL
108   DEPENDS ${LLVM_CONFIG})
110 add_dependencies(llvm-config.target ${llvm_lib_targets})
112 install(FILES ${LLVM_CONFIG}
113   PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
114   WORLD_READ WORLD_EXECUTE
115   DESTINATION bin)