From 86ab76d62468241e03029e3e0790e4a0c8fbe3c1 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Thu, 14 Jul 2011 17:58:33 +0000 Subject: [PATCH] Introduce a CMake build system for Swift. This CMake-based build system is based on the one in Clang, simplified and tweaked slightly to better support building a smaller Xcode project that links against an existing LLVM (rather than importing all of LLVM into this project). Swift SVN r403 --- CMakeLists.txt | 167 +++++++++++++++++++++++++++++++++++++ lib/AST/CMakeLists.txt | 7 ++ lib/CMakeLists.txt | 3 + lib/Parse/CMakeLists.txt | 4 + lib/Sema/CMakeLists.txt | 10 +++ test/lit.site.cfg.in | 21 +++++ tools/swift/CMakeLists.txt | 7 ++ 7 files changed, 219 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 lib/AST/CMakeLists.txt create mode 100644 lib/CMakeLists.txt create mode 100644 lib/Parse/CMakeLists.txt create mode 100644 lib/Sema/CMakeLists.txt create mode 100644 test/lit.site.cfg.in create mode 100644 tools/swift/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000000..de9509f6ef05b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,167 @@ +# Support building Swift as a standalone project, using LLVM as an +# external library. +if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) + project(Swift) + cmake_minimum_required(VERSION 2.8) + + set(SWIFT_PATH_TO_LLVM_SOURCE "" CACHE PATH + "Path to LLVM source code. Not necessary if using an installed LLVM.") + set(SWIFT_PATH_TO_LLVM_BUILD "" CACHE PATH + "Path to the directory where LLVM was built or installed.") + + if( SWIFT_PATH_TO_LLVM_SOURCE ) + if( NOT EXISTS "${SWIFT_PATH_TO_LLVM_SOURCE}/cmake/config-ix.cmake" ) + message(FATAL_ERROR "Please set SWIFT_PATH_TO_LLVM_SOURCE to the root directory of LLVM source code.") + else() + get_filename_component(LLVM_MAIN_SRC_DIR ${SWIFT_PATH_TO_LLVM_SOURCE} + ABSOLUTE) + list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules") + endif() + endif() + + # FIXME: The check for a 'Debug' tblgen is madness + if( NOT EXISTS "${SWIFT_PATH_TO_LLVM_BUILD}/bin/tblgen${CMAKE_EXECUTABLE_SUFFIX}" ) + if ( NOT EXISTS "${SWIFT_PATH_TO_LLVM_BUILD}/bin/Debug/tblgen${CMAKE_EXECUTABLE_SUFFIX}" ) + message(FATAL_ERROR "Please set SWIFT_PATH_TO_LLVM_BUILD to a directory containing an LLVM build.") + endif() + endif() + + list(APPEND CMAKE_MODULE_PATH "${SWIFT_PATH_TO_LLVM_BUILD}/share/llvm/cmake") + + get_filename_component(PATH_TO_LLVM_BUILD ${SWIFT_PATH_TO_LLVM_BUILD} + ABSOLUTE) + + include(AddLLVM) + include(LLVMParseArguments) + include(TableGen) + include("${SWIFT_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake") + include(HandleLLVMOptions) + + set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") + + set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include") + set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR}) + set(LLVM_TOOLS_BINARY_DIR ${CMAKE_BINARY_DIR}) + + set(CMAKE_INCLUDE_CURRENT_DIR ON) + include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}") + link_directories("${PATH_TO_LLVM_BUILD}/lib") + + if( EXISTS "${SWIFT_PATH_TO_LLVM_BUILD}/bin/tblgen${CMAKE_EXECUTABLE_SUFFIX}" ) + set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen") + else() + # FIXME: The fallback to a 'Debug' tblgen is madness + set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/Debug/tblgen") + endif() + + set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) + set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) + set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) + + set( SWIFT_BUILT_STANDALONE 1 ) +endif() + +set(SWIFT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(SWIFT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) + +# Add a new Swift library. +# +# Usage: +# add_swift_library(name # Name of the library (e.g., swiftParse) +# [DEPENDS dep1 ...] # Libraries this library depends on +# [COMPONENT_DEPENDS comp1 ...] # LLVM components this library depends on +# source1 [source2 source3 ...]) # Sources to add into this library +macro(add_swift_library name) + parse_arguments(SWIFTLIB "DEPENDS;COMPONENT_DEPENDS" "" ${ARGN}) + set(SWIFTLIB_SOURCES ${SWIFTLIB_DEFAULT_ARGS}) + + if(MSVC_IDE OR XCODE) + string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) + list( GET split_path -1 dir) + file( GLOB_RECURSE SWIFTLIB_HEADERS + ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.h + ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.td + ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.def) + set(SWIFTLIB_SOURCES ${SWIFTLIB_SOURCES} ${SWIFTLIB_HEADERS}) + endif(MSVC_IDE OR XCODE) + + if (MODULE) + set(libkind MODULE) + elseif (SHARED_LIBRARY) + set(libkind SHARED) + else() + set(libkind) + endif() + + add_library( ${name} ${libkind} ${SWIFTLIB_SOURCES} ) + + if( LLVM_COMMON_DEPENDS ) + add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) + endif( LLVM_COMMON_DEPENDS ) + + target_link_libraries( ${name} ${SWIFTLIB_DEPENDS} ) + llvm_config( ${name} ${SWIFTLIB_COMPONENT_DEPENDS} ) + target_link_libraries( ${name} ${LLVM_COMMON_LIBS} ) + link_system_libs( ${name} ) + + if(MSVC) + get_target_property(cflag ${name} COMPILE_FLAGS) + if(NOT cflag) + set(cflag "") + endif(NOT cflag) + set(cflag "${cflag} /Za") + set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag}) + endif(MSVC) + + install(TARGETS ${name} + LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} + ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) + + set_target_properties(${name} PROPERTIES FOLDER "Swift libraries") +endmacro(add_swift_library) + +# Add a new Swift executable. +# +# Usage: +# add_swift_executable(name # Name of the executable (e.g., swift) +# [DEPENDS dep1 ...] # Libraries this executable depends on +# [COMPONENT_DEPENDS comp1 ...] # LLVM components this executable +# # depends on +# [EXCLUDE_FROM_ALL] # Whether to exclude this executable from +# # the ALL_BUILD target +# source1 [source2 source3 ...]) # Sources to add into this library +macro(add_swift_executable name) + # Parse the arguments we were given. + parse_arguments(SWIFTEXE "DEPENDS;COMPONENT_DEPENDS" "EXCLUDE_FROM_ALL" + ${ARGN}) + + # Add the executable + if (${SWIFTEXE_EXCLUDE_FROM_ALL}) + add_executable(${name} EXCLUDE_FROM_ALL ${SWIFTEXE_DEFAULT_ARGS}) + else() + add_executable(${name} ${SWIFTEXE_DEFAULT_ARGS}) + endif() + + # Add appropriate dependencies + if( LLVM_COMMON_DEPENDS ) + add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) + endif( LLVM_COMMON_DEPENDS ) + + target_link_libraries( ${name} ${SWIFTEXE_DEPENDS} ) + llvm_config( ${name} ${SWIFTEXE_COMPONENT_DEPENDS} ) + target_link_libraries( ${name} ${LLVM_COMMON_LIBS} ) + link_system_libs( ${name} ) + + set_target_properties(${name} PROPERTIES FOLDER "Swift executables") +endmacro(add_swift_executable) + +# We'll need this once we have generated headers +include_directories(BEFORE + ${CMAKE_CURRENT_BINARY_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/include + ) + +# Add all of the subdirectories, where we actually do work. +add_subdirectory(lib) +add_subdirectory(tools) +add_subdirectory(test) diff --git a/lib/AST/CMakeLists.txt b/lib/AST/CMakeLists.txt new file mode 100644 index 0000000000000..38434997b663a --- /dev/null +++ b/lib/AST/CMakeLists.txt @@ -0,0 +1,7 @@ +add_swift_library(swiftAST + ASTContext.cpp + Decl.cpp + Expr.cpp + Identifier.cpp + Type.cpp) + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000000000..a85581e37e884 --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(AST) +add_subdirectory(Sema) +add_subdirectory(Parse) diff --git a/lib/Parse/CMakeLists.txt b/lib/Parse/CMakeLists.txt new file mode 100644 index 0000000000000..0810f02538db7 --- /dev/null +++ b/lib/Parse/CMakeLists.txt @@ -0,0 +1,4 @@ +add_swift_library(swiftParse + Lexer.cpp + Parser.cpp + DEPENDS swiftSema swiftAST) diff --git a/lib/Sema/CMakeLists.txt b/lib/Sema/CMakeLists.txt new file mode 100644 index 0000000000000..814ae09007ef9 --- /dev/null +++ b/lib/Sema/CMakeLists.txt @@ -0,0 +1,10 @@ +add_swift_library(swiftSema + NameBinding.cpp + Scope.cpp + Sema.cpp + SemaBase.cpp + SemaDecl.cpp + SemaExpr.cpp + SemaType.cpp + TypeChecking.cpp + DEPENDS swiftAST) diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in new file mode 100644 index 0000000000000..b09cf913833bf --- /dev/null +++ b/test/lit.site.cfg.in @@ -0,0 +1,21 @@ +## Autogenerated by Swift configuration. +# Do not edit! +config.llvm_src_root = "@LLVM_MAIN_SRC_DIR@" +config.llvm_obj_root = "@LLVM_BINARY_DIR@" +config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" +config.llvm_libs_dir = "@LLVM_LIBS_DIR@" +config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" +config.swift_obj_root = "@SWIFT_BINARY_DIR@" +config.target_triple = "@TARGET_TRIPLE@" + +# Support substitution of the tools and libs dirs with user parameters. This is +# used when we can't determine the tool dir at configuration time. +try: + config.llvm_tools_dir = config.llvm_tools_dir % lit.params + config.llvm_libs_dir = config.llvm_libs_dir % lit.params +except KeyError,e: + key, = e.args + lit.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) + +# Let the main config do the real work. +lit.load_config(config, "@SWIFT_SOURCE_DIR@/test/lit.cfg") diff --git a/tools/swift/CMakeLists.txt b/tools/swift/CMakeLists.txt new file mode 100644 index 0000000000000..2cb91a4212bc3 --- /dev/null +++ b/tools/swift/CMakeLists.txt @@ -0,0 +1,7 @@ +add_swift_executable(swift + swift.cpp + DEPENDS swiftParse swiftSema swiftAST + COMPONENT_DEPENDS bitwriter codegen ipo selectiondag ${LLVM_TARGETS_TO_BUILD}) + +install(TARGETS swift + RUNTIME DESTINATION bin)