Enabling LLVM & Clang to be cross-compiled using CMake from a single configuration...
authorChris Bieneman <beanz@apple.com>
Wed, 3 Sep 2014 23:21:18 +0000 (23:21 +0000)
committerChris Bieneman <beanz@apple.com>
Wed, 3 Sep 2014 23:21:18 +0000 (23:21 +0000)
The basic idea is similar to the existing cross compilation support. A directory must be configured to build host versions of tablegen tools and llvm-config. This directory can be user provided (and configured), or it can be created during the build. During a build the native build directory will be configured and built to supply the tablegen tools used during the build. A user could also explicitly provide the tablegen executables to run on the CMake command line.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217105 91177308-0d34-0410-b5e6-96231b3b80d8

CMakeLists.txt
cmake/modules/CrossCompile.cmake [new file with mode: 0644]
cmake/modules/TableGen.cmake
cmake/platforms/iOS.cmake [new file with mode: 0644]
tools/llvm-config/CMakeLists.txt

index cdaf3bb..f4195ae 100644 (file)
@@ -470,6 +470,11 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
 
 include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
 
+# when crosscompiling import the executable targets from a file
+if(CMAKE_CROSSCOMPILING)
+  include(CrossCompile)
+endif(CMAKE_CROSSCOMPILING)
+
 if( ${CMAKE_SYSTEM_NAME} MATCHES FreeBSD )
   # On FreeBSD, /usr/local/* is not used by default. In order to build LLVM
   # with libxml2, iconv.h, etc., we must add /usr/local paths.
diff --git a/cmake/modules/CrossCompile.cmake b/cmake/modules/CrossCompile.cmake
new file mode 100644 (file)
index 0000000..400381c
--- /dev/null
@@ -0,0 +1,33 @@
+if(NOT DEFINED LLVM_NATIVE_BUILD)
+  set(LLVM_NATIVE_BUILD "${CMAKE_BINARY_DIR}/native")
+  message(STATUS "Setting native build dir to ${LLVM_NATIVE_BUILD}")
+endif(NOT DEFINED LLVM_NATIVE_BUILD)
+
+add_custom_command(OUTPUT ${LLVM_NATIVE_BUILD}
+  COMMAND ${CMAKE_COMMAND} -E make_directory ${LLVM_NATIVE_BUILD}
+  COMMENT "Creating ${LLVM_NATIVE_BUILD}...")
+
+add_custom_command(OUTPUT ${LLVM_NATIVE_BUILD}/CMakeCache.txt
+  COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${CMAKE_SOURCE_DIR}
+  WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
+  DEPENDS ${LLVM_NATIVE_BUILD}
+  COMMENT "Configuring native LLVM...")
+
+add_custom_target(ConfigureNativeLLVM DEPENDS ${LLVM_NATIVE_BUILD}/CMakeCache.txt)
+
+set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${LLVM_NATIVE_BUILD})
+
+if(NOT IS_DIRECTORY ${LLVM_NATIVE_BUILD})
+  if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
+    set(HOST_SYSROOT_FLAGS -DCMAKE_OSX_SYSROOT=macosx)
+  endif(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
+
+  message(STATUS "Configuring native build...")
+  execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory
+    ${LLVM_NATIVE_BUILD} )
+
+  message(STATUS "Configuring native targets...")
+  execute_process(COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release
+      -G "${CMAKE_GENERATOR}" -DLLVM_TARGETS_TO_BUILD=${LLVM_TARGETS_TO_BUILD} ${HOST_SYSROOT_FLAGS} ${CMAKE_SOURCE_DIR}
+    WORKING_DIRECTORY ${LLVM_NATIVE_BUILD} )
+endif(NOT IS_DIRECTORY ${LLVM_NATIVE_BUILD})
index 845c986..67031a5 100644 (file)
@@ -70,35 +70,6 @@ function(add_public_tablegen_target target)
   set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${target} PARENT_SCOPE)
 endfunction()
 
-if(CMAKE_CROSSCOMPILING)
-  set(CX_NATIVE_TG_DIR "${CMAKE_BINARY_DIR}/native")
-
-  add_custom_command(OUTPUT ${CX_NATIVE_TG_DIR}
-    COMMAND ${CMAKE_COMMAND} -E make_directory ${CX_NATIVE_TG_DIR}
-    COMMENT "Creating ${CX_NATIVE_TG_DIR}...")
-
-  # Forward a subset of configure options to discover additional tablegen modules.
-  get_cmake_property(_variableNames CACHE_VARIABLES)
-  foreach (_variableName ${_variableNames})
-    if (_variableName MATCHES "^(LLVM_EXTERNAL_.*_SOURCE_DIR)$")
-      list(APPEND CX_CMAKE_ARGUMENTS "-D${_variableName}=\"${${_variableName}}\"")
-    endif ()
-  endforeach()
-
-  add_custom_command(OUTPUT ${CX_NATIVE_TG_DIR}/CMakeCache.txt
-    # TODO: Clear the old CMakeCache.txt somehow without breaking restat.
-    COMMAND ${CMAKE_COMMAND} -UMAKE_TOOLCHAIN_FILE -DCMAKE_BUILD_TYPE=Release
-                             -DLLVM_BUILD_POLLY=OFF ${CX_CMAKE_ARGUMENTS}
-                             -G "${CMAKE_GENERATOR}" ${CMAKE_SOURCE_DIR}
-    WORKING_DIRECTORY ${CX_NATIVE_TG_DIR}
-    DEPENDS ${CX_NATIVE_TG_DIR}
-    COMMENT "Configuring native TableGen...")
-
-  add_custom_target(ConfigureNativeTableGen DEPENDS ${CX_NATIVE_TG_DIR}/CMakeCache.txt)
-
-  set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CX_NATIVE_TG_DIR})
-endif()
-
 macro(add_tablegen target project)
   set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
   set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
@@ -122,16 +93,16 @@ macro(add_tablegen target project)
 
   if(CMAKE_CROSSCOMPILING)
     if( ${${project}_TABLEGEN} STREQUAL "${target}" )
-      set(${project}_TABLEGEN_EXE "${CX_NATIVE_TG_DIR}/bin/${target}")
+      set(${project}_TABLEGEN_EXE "${LLVM_NATIVE_BUILD}/bin/${target}")
       set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
 
       add_custom_command(OUTPUT ${${project}_TABLEGEN_EXE}
-        COMMAND ${CMAKE_BUILD_TOOL} ${target}
-        DEPENDS ${CX_NATIVE_TG_DIR}/CMakeCache.txt
-        WORKING_DIRECTORY ${CX_NATIVE_TG_DIR}
+        COMMAND ${CMAKE_COMMAND} --build . --target ${target} --config $<CONFIGURATION>
+        DEPENDS ${LLVM_NATIVE_BUILD}/CMakeCache.txt
+        WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
         COMMENT "Building native TableGen...")
       add_custom_target(${project}NativeTableGen DEPENDS ${${project}_TABLEGEN_EXE})
-      add_dependencies(${project}NativeTableGen ConfigureNativeTableGen)
+      add_dependencies(${project}NativeTableGen ConfigureNativeLLVM)
 
       add_dependencies(${target} ${project}NativeTableGen)
     endif()
diff --git a/cmake/platforms/iOS.cmake b/cmake/platforms/iOS.cmake
new file mode 100644 (file)
index 0000000..4973643
--- /dev/null
@@ -0,0 +1,47 @@
+# Toolchain config for iOS.
+#
+# Usage:
+# mkdir build; cd build
+# cmake ..; make
+# mkdir ios; cd ios
+# cmake -DLLVM_IOS_TOOLCHAIN_DIR=/path/to/ios/ndk \
+#   -DCMAKE_TOOLCHAIN_FILE=../../cmake/platforms/iOS.cmake ../..
+# make <target>
+
+SET(CMAKE_SYSTEM_NAME Darwin)
+SET(CMAKE_SYSTEM_VERSION 13)
+SET(CMAKE_CXX_COMPILER_WORKS True)
+SET(CMAKE_C_COMPILER_WORKS True)
+SET(DARWIN_TARGET_OS_NAME ios)
+
+IF(NOT DEFINED ENV{SDKROOT})
+ MESSAGE(FATAL_ERROR "SDKROOT env var must be set: " $ENV{SDKROOT})
+ENDIF()
+
+IF(NOT CMAKE_C_COMPILER)
+  execute_process(COMMAND xcrun -sdk iphoneos -find clang
+   OUTPUT_VARIABLE CMAKE_C_COMPILER
+   ERROR_QUIET
+   OUTPUT_STRIP_TRAILING_WHITESPACE)
+  message(STATUS "Using c compiler ${CMAKE_C_COMPILER}")
+ENDIF()
+
+IF(NOT CMAKE_CXX_COMPILER)
+  execute_process(COMMAND xcrun -sdk iphoneos -find clang++
+   OUTPUT_VARIABLE CMAKE_CXX_COMPILER
+   ERROR_QUIET
+   OUTPUT_STRIP_TRAILING_WHITESPACE)
+  message(STATUS "Using c compiler ${CMAKE_CXX_COMPILER}")
+ENDIF()
+
+IF (NOT DEFINED IOS_MIN_TARGET)
+execute_process(COMMAND xcodebuild -sdk iphoneos -version SDKVersion
+   OUTPUT_VARIABLE IOS_MIN_TARGET
+   ERROR_QUIET
+   OUTPUT_STRIP_TRAILING_WHITESPACE)
+ENDIF()
+
+SET(IOS_COMMON_FLAGS "-isysroot $ENV{SDKROOT} -mios-version-min=${IOS_MIN_TARGET}")
+SET(CMAKE_C_FLAGS "${IOS_COMMON_FLAGS}" CACHE STRING "toolchain_cflags" FORCE)
+SET(CMAKE_CXX_FLAGS "${IOS_COMMON_FLAGS}" CACHE STRING "toolchain_cxxflags" FORCE)
+SET(CMAKE_LINK_FLAGS "${IOS_COMMON_FLAGS}" CACHE STRING "toolchain_linkflags" FORCE)
index 8d83762..50c84e6 100644 (file)
@@ -33,3 +33,18 @@ add_llvm_tool(llvm-config
 
 # Add the dependency on the generation step.
 add_file_dependencies(${CMAKE_CURRENT_SOURCE_DIR}/llvm-config.cpp ${BUILDVARIABLES_OBJPATH})
+
+if(CMAKE_CROSSCOMPILING)
+  set(${project}_LLVM_CONFIG_EXE "${LLVM_NATIVE_BUILD}/bin/llvm-config")
+  set(${project}_LLVM_CONFIG_EXE ${${project}_LLVM_CONFIG_EXE} PARENT_SCOPE)
+
+  add_custom_command(OUTPUT "${${project}_LLVM_CONFIG_EXE}"
+    COMMAND ${CMAKE_COMMAND} --build . --target llvm-config --config $<CONFIGURATION>
+    DEPENDS ${LLVM_NATIVE_BUILD}/CMakeCache.txt
+    WORKING_DIRECTORY ${LLVM_NATIVE_BUILD}
+    COMMENT "Building native llvm-config...")
+  add_custom_target(${project}NativeLLVMConfig DEPENDS ${${project}_LLVM_CONFIG_EXE})
+  add_dependencies(${project}NativeLLVMConfig ConfigureNativeLLVM)
+
+  add_dependencies(llvm-config ${project}NativeLLVMConfig)
+endif(CMAKE_CROSSCOMPILING)