Skip to content

Commit

Permalink
[cmake] Add CcToolchain.cmake to support bootstrapping the project.
Browse files Browse the repository at this point in the history
- [azure] Enable bootstrapping the project.
- [azure] Install libc++ before building.
- [cmake] Do not use the $<TARGET_FILE_DIR:tgt> expression.
  • Loading branch information
disigma committed Jun 21, 2019
1 parent b9e4341 commit 9d87f95
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 4 deletions.
16 changes: 13 additions & 3 deletions .ci/azure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,24 @@ jobs:
pool:
vmImage: 'Ubuntu-latest'
steps:
- script:
wget -O $(Agent.TempDirectory)/libc++abi.deb
http://apt.llvm.org/trusty/pool/main/l/llvm-toolchain-8/libc++abi1-8_8.0.1~svn360950-1~exp1~20190517011409.67_amd64.deb
displayName: 'Downloading libc++abi'
- script:
wget -O $(Agent.TempDirectory)/libc++.deb
http://apt.llvm.org/trusty/pool/main/l/llvm-toolchain-8/libc++1-8_8.0.1~svn360950-1~exp1~20190517011409.67_amd64.deb
displayName: 'Downloading libc++'
- script:
sudo dpkg -i $(Agent.TempDirectory)/libc++abi.deb $(Agent.TempDirectory)/libc++.deb
displayName: 'Installing libc++'
- script:
cmake -E make_directory cmake-build-azure
displayName: 'Prepare project'
- script:
cmake -E chdir cmake-build-azure cmake ..
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=clang
-DCMAKE_CXX_COMPILER=clang++
-DCMAKE_BUILD_TYPE=MinSizeRel
-DCMAKE_TOOLCHAIN_FILE=../cmake/CcToolchain.cmake
-DCMAKE_INSTALL_PREFIX="$(Build.BinariesDirectory)/wimal"
displayName: 'Configure project'
- script:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ __pycache__/
!/libxml2/include/
!/libxml2/*.[c|h]
/libxml2/**/Makefile.am

### wimal
/cmake/.cache
/cmake/wimal
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(LLVM_ENABLE_ZLIB ON CACHE BOOL LLVM_ENABLE_ZLIB)
set(HAVE_LIBZ_Z 1 CACHE INTERNAL HAVE_LIBZ_Z)
set(HAVE_ZLIB_H 1 CACHE INTERNAL HAVE_ZLIB_H)
include_directories($<TARGET_PROPERTY:zlibstatic,INCLUDE_DIRECTORIES>)
link_directories($<TARGET_FILE_DIR:zlibstatic>)
link_directories("${CMAKE_CURRENT_BINARY_DIR}/zlib")

# Add libxml2
add_subdirectory(libxml2)
Expand Down
126 changes: 126 additions & 0 deletions cmake/CcToolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
cmake_minimum_required(VERSION 3.0)

function(cc_install_toolchain)
set(CACHE_DIR "${CMAKE_CURRENT_LIST_DIR}/.cache")
# Remove cache directory if exists.
if(EXISTS "${CACHE_DIR}")
message(STATUS "Removing ${CACHE_DIR}")
file(REMOVE_RECURSE "${CACHE_DIR}")
endif()
# Create cache directory.
message(STATUS "Creating ${CACHE_DIR}")
file(MAKE_DIRECTORY "${CACHE_DIR}")
# Fetch latest release.
set(RELEASE_URL https://github.com/wimal-build/wimal/releases)
message(STATUS "Fetching ${RELEASE_URL}/latest")
file(
DOWNLOAD
"${RELEASE_URL}/latest"
"${CACHE_DIR}/releases"
)
file(READ "${CACHE_DIR}/releases" releases)
# Parse the latest version and filenames.
if(releases MATCHES "/download/([^/]+)/(wimal-linux[^\"]*.tar.xz)")
set(wimal-version ${CMAKE_MATCH_1})
set(wimal-toolchain ${CMAKE_MATCH_2})
message(STATUS "Latest version: ${wimal-version}")
message(STATUS "Toolchain: ${wimal-toolchain}")
else()
message(FATAL_ERROR "No releases found")
exit()
endif()
if(releases MATCHES "/download/${wimal-version}/(wimal-sysroot[^\"]*.tar.xz)")
set(wimal-sysroot ${CMAKE_MATCH_1})
else()
message(FATAL_ERROR "No releases found")
endif()
message(STATUS "Sysroot: ${wimal-sysroot}")
# Download the toolchain tarball.
set(url "${RELEASE_URL}/download/${wimal-version}/${wimal-toolchain}")
message(STATUS "Downloading: ${url}")
file(DOWNLOAD "${url}" "${CACHE_DIR}/toolchain.tar.xz" SHOW_PROGRESS STATUS status)
list(GET status 0 error)
list(GET status 1 message)
if(error)
message(FATAL_ERROR "Failed to download, error=${error} message=${message}")
endif()
# Download the sysroot tarball.
set(url "${RELEASE_URL}/download/${wimal-version}/${wimal-sysroot}")
message(STATUS "Downloading: ${url}")
file(DOWNLOAD "${url}" "${CACHE_DIR}/sysroot.tar.xz" SHOW_PROGRESS STATUS status)
list(GET status 0 error)
list(GET status 1 message)
if(error)
message(FATAL_ERROR "Failed to download, error=${error} message=${message}")
endif()
# Clean old wimal installation.
set(WIMAL_DIR "${CMAKE_CURRENT_LIST_DIR}/wimal")
if(EXISTS "${WIMAL_DIR}")
message(STATUS "Removing ${WIMAL_DIR}")
file(REMOVE_RECURSE "${WIMAL_DIR}")
endif()
# Extract toolchain.
message(STATUS "Extracting ${CACHE_DIR}/toolchain.tar.xz")
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar Jxf "${CACHE_DIR}/toolchain.tar.xz"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
RESULT_VARIABLE error
)
if(error)
message(FATAL_ERROR "Failed to extract ${CACHE_DIR}/toolchain.tar.xz")
endif()
# Extract toolchain.
message(STATUS "Extracting ${CACHE_DIR}/sysroot.tar.xz")
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar Jxf "${CACHE_DIR}/sysroot.tar.xz"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/wimal"
RESULT_VARIABLE error
)
if(error)
message(FATAL_ERROR "Failed to extract ${CACHE_DIR}/toolchain.tar.xz")
endif()
# Cleanup cache directory.
file(REMOVE_RECURSE "${CACHE_DIR}")
# Install wimal.
message(STATUS "Installing wimal")
execute_process(
COMMAND "${CMAKE_CURRENT_LIST_DIR}/wimal/bin/wimal" install
RESULT_VARIABLE error
)
if(error)
message(FATAL_ERROR "Failed to install wimal")
endif()
endfunction(cc_install_toolchain)

if(CMAKE_SCRIPT_MODE_FILE STREQUAL CMAKE_CURRENT_LIST_FILE)
cc_install_toolchain()
return()
endif()

if(NOT WIMAL_HOME)
set(WIMAL_HOME $ENV{WIMAL_HOME})
endif()

if(NOT WIMAL_HOME)
set(WIMAL_HOME "${CMAKE_CURRENT_LIST_DIR}/wimal")
endif()

if(NOT EXISTS "${WIMAL_HOME}/bin/wimal")
if(EXISTS "/wimal/bin/wimal")
set(WIMAL_HOME "/wimal")
else(DEFINED ENV{WIMAL_HOME})
message(STATUS "wimal not found, installing")
cc_install_toolchain()
endif()
endif()

if(NOT EXISTS "${WIMAL_HOME}/bin/wimal")
message(FATAL_ERROR "wimal not found in ${WIMAL_HOME}/bin/wimal")
endif()

if(NOT WIMAL_TARGET)
set(WIMAL_TARGET x64-linux)
endif()

set(CMAKE_C_COMPILER "${WIMAL_HOME}/bin/x64-linux-cc")
set(CMAKE_CXX_COMPILER "${WIMAL_HOME}/bin/x64-linux-c++")

0 comments on commit 9d87f95

Please sign in to comment.