Skip to content

Instantly share code, notes, and snippets.

@bl4ckb0ne
Created February 22, 2018 21:59
Show Gist options
  • Save bl4ckb0ne/f1ac983423eb6f3018058f435ad966e1 to your computer and use it in GitHub Desktop.
Save bl4ckb0ne/f1ac983423eb6f3018058f435ad966e1 to your computer and use it in GitHub Desktop.
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR "x86_64")
# Because build platform is different than target platform
set(CMAKE_CROSSCOMPILING TRUE)
set(TOOLCHAIN_USED "NI LinuxRT SDK 2017")
# Generator is force to Unix Makefiles to generate a makefiles
# Otherwise Visual Studio generator is there by default because
# we are on a Windows platform
if(NOT CMAKE_GENERATOR MATCHES "Unix Makefiles")
message("Generator : ${CMAKE_GENERATOR}")
message(FATAL_ERROR "NI toolchain requires Unix makefile. Add '-G \"Unix Makefiles\"' to your command")
endif(NOT CMAKE_GENERATOR MATCHES "Unix Makefiles")
# We skip compiler identification because the standard check cannot compile without --sysroot
# https://stackoverflow.com/questions/41589430/cmake-c-compiler-identification-fails
set(CMAKE_C_COMPILER_WORKS TRUE CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_WORKS TRUE CACHE INTERNAL "")
set(CMAKE_C_COMPILER_FORCED TRUE CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_FORCED TRUE CACHE INTERNAL "")
set(CMAKE_C_COMPILER_ID_RUN TRUE CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_ID_RUN TRUE CACHE INTERNAL "")
# Since we forced the compiler, CMAKE_SIZEOF_VOID_P is not set by default
set(CMAKE_SIZEOF_VOID_P 8 CACHE INTERNAL "")
set(NI_SYSROOT_PATH "C:/build/17.0/x64/sysroots")
# https://cmake.org/cmake/help/v3.0/variable/CMAKE_SYSROOT.html
set(CMAKE_SYSROOT "${NI_SYSROOT_PATH}/core2-64-nilrt-linux" CACHE INTERNAL "")
set(CMAKE_SYSROOT_LINK "${NI_SYSROOT_PATH}/core2-64-nilrt-linux" CACHE INTERNAL "")
set(CMAKE_SYSROOT_COMPILE "${NI_SYSROOT_PATH}/core2-64-nilrt-linux" CACHE INTERNAL "")
# CMAKE_SYSROOT setup seems broken, so we need to force the value
add_compile_options(--sysroot=${CMAKE_SYSROOT})
set(CMAKE_EXE_LINKER_FLAGS_INIT "--sysroot=${CMAKE_SYSROOT}" CACHE STRING "" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS_INIT "--sysroot=${CMAKE_SYSROOT}" CACHE STRING "" FORCE)
# Don't override the static linker flags because it will also be shared to the ar
# https://cmake.org/pipermail/cmake/2016-January/062447.html
#set(CMAKE_STATIC_LINKER_FLAGS_INIT "--sysroot=${CMAKE_SYSROOT}" CACHE STRING "" FORCE)
# https://cmake.org/cmake/help/v3.10/variable/CMAKE_FIND_ROOT_PATH.html
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}" CACHE INTERNAL "")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
# Path to programs. FORCE is used because those variables are flushed somewhere in the process
set(NI_SYSROOT_BIN_PATH "${NI_SYSROOT_PATH}/i686-nilrtsdk-mingw32/usr/bin/x86_64-nilrt-linux")
set(CMAKE_AR "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-ar.exe" CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-g++.exe" CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_AR "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-gcc-ar.exe" CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_RANLIB "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-gcc-ranlib.exe" CACHE INTERNAL "")
set(CMAKE_C_COMPILER "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-gcc.exe" CACHE INTERNAL "")
set(CMAKE_C_COMPILER_AR "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-gcc-ar.exe" CACHE INTERNAL "")
set(CMAKE_C_COMPILER_RANLIB "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-gcc-ranlib.exe" CACHE INTERNAL "")
set(CMAKE_LINKER "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-ld.exe" CACHE INTERNAL "")
set(CMAKE_NM "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-nm.exe" CACHE INTERNAL "")
set(CMAKE_OBJCOPY "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-objcpy.exe" CACHE INTERNAL "")
set(CMAKE_OBJDUMP "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-objdump.exe" CACHE INTERNAL "")
set(CMAKE_STRIP "${NI_SYSROOT_BIN_PATH}/x86_64-nilrt-linux-strip.exe" CACHE INTERNAL "")
# Set flags for C & CXX
# TODO look into -fipa-* options in gcc for equivalence with icc's interprocedural optimization flag (-ip)
set(CMAKE_FLAGS "-Wpointer-arith -Wall -fno-exceptions -freciprocal-math -fstrict-aliasing -fipa-pure-const -fipa-reference -fipa-profile -fipa-cp -fno-rounding-math -Wno-attributes -Wno-unused-but-set-variable")
set(CMAKE_C_FLAGS_INIT "${CMAKE_FLAGS}")
set(CMAKE_CXX_FLAGS_INIT "${CMAKE_FLAGS} -std=c++0x")
if(CMAKE_BUILD_TYPE STREQUAL Release)
set(CMAKE_FLAGS_RELEASE " -O3")
set(CMAKE_C_FLAGS_RELEASE_INIT "${CMAKE_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${CMAKE_FLAGS_RELEASE}")
add_definitions(-DFOR_RELEASE -DNDEBUG -DUSE_RESTRICT)
elseif(CMAKE_BUILD_TYPE STREQUAL Debug)
set(CMAKE_FLAGS_DEBUG " -O0 -g")
set(CMAKE_C_FLAGS_DEBUG_INIT "${CMAKE_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${CMAKE_FLAGS_DEBUG}")
add_definitions(-DFOR_DEBUG -DDEBUG -D_DEBUG)
endif()
# Defines for everything
add_definitions(-D_GNU_SOURCE -D_STANDALONE -DRT -DFOR_NI)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment