Skip to content

Commit

Permalink
build system: switch to cmake
Browse files Browse the repository at this point in the history
This commit removes the old build system at the same time, because
maintaining two build systems in parallel does not work.

Makefiles were kept in directories not reimplemented yet (e.g. builtin)

Change-Id: I48f252b3267bbecff6e1d8ea25a64038074f1568
  • Loading branch information
Dominique Martinet authored and martinetd committed Feb 14, 2019
1 parent 2ebc029 commit b7c9bb6
Show file tree
Hide file tree
Showing 30 changed files with 395 additions and 6,029 deletions.
148 changes: 148 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
cmake_minimum_required(VERSION 2.6)

if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type: Debug Release..." FORCE)
endif (NOT CMAKE_BUILD_TYPE)

# C flags need to be set before enabling language?
set(CMAKE_C_FLAGS_DEBUG "-g -Wall -Wextra -Wno-unused-parameter -Wno-sign-compare" CACHE STRING "Debug compiler flags")
set(CMAKE_C_FLAGS_RELEAES "-Wall" CACHE STRING "Release compiler flags")

enable_language(C)

project(ihk C)
set(IHK_VERSION "1.6.0")

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
include(GNUInstallDirs)
include(CMakeParseArguments)
include(Kbuild)
include(Ksym)

if (CMAKE_INSTALL_PREFIX STREQUAL "/usr")
set(KMODDIR "/lib/modules/${UNAME_R}/extra/mckernel")
else()
set(KMODDIR "${CMAKE_INSTALL_PREFIX}/kmod")
endif()

# build options
option(ENABLE_WERROR "Enable -Werror" OFF)
if (ENABLE_WERROR)
add_compile_options("-Werror")
endif(ENABLE_WERROR)
set(BUILD_TARGET "smp-x86" CACHE STRING "Build target: smp-x86 | smp-arm64")
set_property(CACHE BUILD_TARGET PROPERTY STRINGS smp-x86 smp-arm64)

if (BUILD_TARGET STREQUAL "smp-x86")
option(ENABLE_PERF "Enable perf support" ON)
set(ARCH "x86_64")
elseif (BUILD_TARGET STREQUAL "smp-arm64")
option(ENABLE_PERF "Enable perf support" OFF)
foreach(i RANGE 1 120)
add_definitions(-DPOSTK_DEBUG_ARCH_DEP_${i} -DPOSTK_DEBUG_TEMP_FIX_${i})
set(KBUILD_C_FLAGS "${KBUILD_C_FLAGS} -DPOSTK_DEBUG_ARCH_DEP_${i} -DPOSTK_DEBUG_TEMP_FIX_${i}")
endforeach()
set(ARCH "arm64")
endif()

set(ENABLE_MEMDUMP AUTO CACHE STRING "Enable memory dump support")
set_property(CACHE ENABLE_MEMDUMP PROPERTY STRINGS AUTO ON OFF)
if (ENABLE_MEMDUMP STREQUAL AUTO)
if (BUILD_TARGET MATCHES "smp-.*")
set(ENABLE_MEMDUMP ON)
else()
set(ENABLE_MEMDUMP OFF)
endif()
endif(ENABLE_MEMDUMP STREQUAL AUTO)
if (ENABLE_MEMDUMP)
# XXX check libiberty?
endif(ENABLE_MEMDUMP)
# bfd seems always needed, so check always.
find_library(LIBBFD bfd)
find_library(LIBIBERTY iberty)
find_library(LIBUDEV udev)

option(ENABLE_PERF "Enable perf support" ON)
option(ENABLE_RUSAGE "Enable rusage support" ON)

# actual build section - just subdirs
add_subdirectory("linux/core")
add_subdirectory("linux/user")
if(BUILD_TARGET STREQUAL "attached-mic")
#add_subdirectory("linux/driver/attached/mic")
elseif(BUILD_TARGET MATCHES "builtin.*")
#add_subdirectory("linux/driver/builtin")
elseif(BUILD_TARGET MATCHES "smp.*")
add_subdirectory("linux/driver/smp")
else()
message(FATAL_ERROR "Invalid target ${BUILD_TARGET}")
endif()

# rest of config.h
execute_process(COMMAND git --git-dir=${PROJECT_SOURCE_DIR}/.git rev-parse --short HEAD
OUTPUT_VARIABLE BUILDID OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
if(BUILDID STREQUAL "")
set(BUILDID ${IHK_VERSION})
endif()
# also set BUILDID for mckernel
set(BUILDID ${BUILDID} PARENT_SCOPE)

if(BUILD_TARGET STREQUAL "smp-x86")
ksym(x86_trampoline_base PREFIX IHK_)
ksym(real_mode_header PREFIX IHK_)
ksym(per_cpu__vector_irq PREFIX IHK_)
ksym(vector_irq PREFIX IHK_)
ksym(lapic_get_maxlvt PREFIX IHK_)
ksym(init_deasserted PREFIX IHK_)
ksym(irq_to_desc PREFIX IHK_)
ksym(alloc_desc PREFIX IHK_)
ksym(irq_desc_tree PREFIX IHK_)
ksym(irq_to_desc_alloc_node PREFIX IHK_)
ksym(dummy_irq_chip PREFIX IHK_)
ksym(get_uv_system_type PREFIX IHK_)
ksym(wakeup_secondary_cpu_via_init PREFIX IHK_)
ksym(__default_send_IPI_dest_field PREFIX IHK_)
ksym(vmap_area_root PREFIX IHK_)
ksym(vmap_area_lock PREFIX IHK_)
ksym(__insert_vmap_area PREFIX IHK_)
ksym(__free_vmap_area PREFIX IHK_)

elseif(BUILD_TARGET STREQUAL "smp-arm64")
ksym(gic_data PREFIX IHK_ SOURCE_FILE irq-gic.c SUFFIX _gicv2)
ksym(gic_data PREFIX IHK_ SOURCE_FILE irq-gic-v3.c SUFFIX _gicv3)
ksym(gic_raise_softirq PREFIX IHK_ SOURCE_FILE irq-gic.c SUFFIX _gicv2)
ksym(gic_raise_softirq PREFIX IHK_ SOURCE_FILE irq-gic-v3.c SUFFIX _gicv3)
ksym(__irq_domain_alloc_irqs PREFIX IHK_)
ksym(irq_domain_free_irqs PREFIX IHK_)
ksym(irq_to_desc PREFIX IHK_)
ksym(alloc_desc PREFIX IHK_)
ksym(irq_desc_tree PREFIX IHK_)
ksym(irq_to_desc_alloc_node PREFIX IHK_)
ksym(psci_ops PREFIX IHK_)
ksym(__cpu_logical_map PREFIX IHK_)
ksym(invoke_psci_fn PREFIX IHK_)
ksym(__invoke_psci_fn_hvc PREFIX IHK_)
ksym(__invoke_psci_fn_smc PREFIX IHK_)
ksym(acpi_gic_ver PREFIX IHK_)
ksym(cpu_pmu PREFIX IHK_)
ksym(__oprofile_cpu_pmu PREFIX IHK_)
ksym(__irq_set_affinity PREFIX IHK_)
ksym(arch_timer_use_virtual PREFIX IHK_)
ksym(arch_timer_uses_ppi PREFIX IHK_)
ksym(arch_timer_rate PREFIX IHK_)
endif()



configure_file(config.h.in config.h)


# config report only if standalone
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
message("Build type: ${CMAKE_BUILD_TYPE}")
message("Build target: ${BUILD_TARGET}")
message("ENABLE_MEMDUMP: ${ENABLE_MEMDUMP}")
message("ENABLE_PERF: ${ENABLE_PERF}")
message("ENABLE_RUSAGE: ${ENABLE_RUSAGE}")
message("ENABLE_WERROR: ${ENABLE_WERROR}")
endif()
65 changes: 0 additions & 65 deletions Makefile.in

This file was deleted.

10 changes: 10 additions & 0 deletions cmake/cross-aarch64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SET(CMAKE_SYSTEM_NAME Linux)

SET(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)

SET(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu/sys-root)

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
100 changes: 100 additions & 0 deletions cmake/modules/Kbuild.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Interface to kbuild
#
# Generate Kbuild file as appropriate and call make to the kernel build system
# Original goal was to be simple, but correctness is difficult...

set(UNAME_R ${CMAKE_SYSTEM_VERSION} CACHE STRING "Kernel version to build against")
set(KERNEL_DIR "/lib/modules/${UNAME_R}/build" CACHE STRING "kernel build directory")

set(KBUILD_C_FLAGS "" CACHE STRING "Compiler flags to give to Kbuild.")
set(KBUILD_MAKE_FLAGS "" CACHE STRING "Extra make arguments for Kbuild.")

mark_as_advanced(
KBUILD_C_FLAGS
KBUILD_MAKE_FLAGS
)

function(kmod MODULE_NAME)
cmake_parse_arguments(KMOD "" "INSTALL_DEST" "C_FLAGS;SOURCES;EXTRA_SYMBOLS;DEPENDS" ${ARGN})

add_custom_target(${MODULE_NAME}_ko ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}.ko"
"${CMAKE_CURRENT_BINARY_DIR}/Module.symvers")

string(REGEX REPLACE "\\.c(;|$)" ".o\\1" KMOD_OBJECTS "${KMOD_SOURCES}")
string(REPLACE ";" " " OBJECTS "${KMOD_OBJECTS}")
string(REPLACE ";" " " C_FLAGS "${KMOD_C_FLAGS}")
string(REPLACE ";" " " EXTRA_SYMBOLS "${KMOD_EXTRA_SYMBOLS}")
if(ENABLE_WERROR)
set(ccflags "${KBUILD_C_FLAGS} ${C_FLAGS} -Werror")
else(ENABLE_WERROR)
set(ccflags "${KBUILD_C_FLAGS} ${C_FLAGS}")
endif(ENABLE_WERROR)
configure_file(${CMAKE_SOURCE_DIR}/cmake/modules/Kbuild.in
${CMAKE_CURRENT_BINARY_DIR}/Kbuild)

if (${CMAKE_GENERATOR} STREQUAL Ninja)
set(MAKE "make")
list(APPEND KBUILD_MAKE_FLAGS "-j")
else ()
set(MAKE "$(MAKE)")
endif ()
if (NOT "${ARCH}" STREQUAL "${CMAKE_HOST_SYSTEM_PROCESSOR}")
string(REGEX REPLACE "ld$" "" CROSS_COMPILE "${CMAKE_LINKER}")
list(APPEND KBUILD_MAKE_FLAGS "ARCH=${ARCH};CROSS_COMPILE=${CROSS_COMPILE}")
endif()

string(REGEX REPLACE "\\.c(;|$)" ".o.cmd\\1" KMOD_O_CMD "${KMOD_SOURCES}")
string(REGEX REPLACE "[^/;]+(;|$)" ".\\0" KMOD_O_CMD "${KMOD_O_CMD}")


# This custom command has two uses:
# - first is to list kbuild output files, so make clean does something
# (cmake does not let us add a custom command to make clean)
# - this alone could have been added to the other command, but cmake insists
# on messing with timestamps with touch_nocreate after the command runs,
# so it would incorrectly make intermediary outputs newer than the .ko
# and force kbuild to relink needlessly
add_custom_command(
OUTPUT
old_timestamp
${KMOD_OBJECTS}
${KMOD_O_CMD}
"${MODULE_NAME}.o"
".${MODULE_NAME}.o.cmd"
"${MODULE_NAME}.mod.c"
"${MODULE_NAME}.mod.o"
".${MODULE_NAME}.mod.o.cmd"
".${MODULE_NAME}.ko.cmd"
".tmp_versions/${MODULE_NAME}.mod"
".tmp_versions"
"modules.order"
COMMAND touch old_timestamp
)

# This custom command forces cmake to rebuild the module, so kbuild's dependencies
# (including header files modifications) kick in everytime.
# Ideally, should later be replaced by something parsing the .xxx.cmd files to have
# the native build system do these checks, if possible at all...
add_custom_command(OUTPUT kmod_always_rebuild COMMAND touch kmod_always_rebuild)

add_custom_command(
OUTPUT "${MODULE_NAME}.ko"
"Module.symvers"
COMMAND ${MAKE} ${KBUILD_MAKE_FLAGS} -C ${KERNEL_DIR}
M=${CMAKE_CURRENT_BINARY_DIR} modules
COMMAND rm -f kmod_always_rebuild
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/Kbuild"
${KMOD_DEPENDS}
kmod_always_rebuild
old_timestamp
COMMENT "Building kmod ${MODULE_NAME}"
)

if (KMOD_INSTALL_DEST)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}.ko"
DESTINATION "${KMOD_INSTALL_DEST}")
endif (KMOD_INSTALL_DEST)

message("Defined module ${MODULE_NAME}")
endfunction(kmod)
7 changes: 7 additions & 0 deletions cmake/modules/Kbuild.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ccflags-y := @ccflags@
src := @CMAKE_CURRENT_SOURCE_DIR@

KBUILD_EXTRA_SYMBOLS := @EXTRA_SYMBOLS@

obj-m := @[email protected]
@MODULE_NAME@-y := @OBJECTS@
43 changes: 43 additions & 0 deletions cmake/modules/Ksym.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Lookup symbol addresses from Ksymbol file

set(SYSTEM_MAP "${KERNEL_DIR}/System.map" CACHE STRING "System map to look for symbols")
set(VMLINUX "${KERNEL_DIR}/vmlinux" CACHE STRING "kernel object file")


function(ksym SYMBOL)
cmake_parse_arguments(KSYM "" "PREFIX;SOURCE_FILE;SUFFIX" "" ${ARGN})

execute_process(COMMAND awk "/ ${SYMBOL}$/ { print $1 }" ${SYSTEM_MAP}
OUTPUT_VARIABLE ADDRESS_CANDIDATES OUTPUT_STRIP_TRAILING_WHITESPACE)

if (NOT ADDRESS_CANDIDATES)
return()
endif()

# listify and get first element
string(REPLACE "\n" ";" ADDRESS_CANDIDATES "${ADDRESS_CANDIDATES}")
list(GET ADDRESS_CANDIDATES 0 ADDRESS)

if (SOURCE_FILE)
foreach(ADDRESS IN LISTS ADDRESS_CANDIDATES)
execute_process(COMMAND addr2line -e ${VMLINUX} ${ADDRESS}
OUTPUT_VARIABLE LINE OUTPUT_STRIP_TRAILING_WHITESPACE)
if(LINE MATCHES ".*${SOURCE_FILE}:.*")
set(FOUND ADDRESS)
break()
endif()
endforeach(ADDRESS)
if(NOT FOUND)
return()
endif()

# ?! why only if source_file?...
execute_process(COMMAND "awk '/ __ksymtab_${SYMBOL}$/ { print $1 }'"
OUTPUT_VARIABLE SYMBOL_EXPORTED OUTPUT_STRIP_TRAILING_WHITESPACE)
if (SYMBOL_EXPORTED)
set(ADDRESS 0)
endif(SYMBOL_EXPORTED)
endif(SOURCE_FILE)

set(${KSYM_PREFIX}KSYM_${SYMBOL}${KSYM_SUFFIX} "0x${ADDRESS}" CACHE INTERNAL "symbol")
endfunction(ksym)
Loading

0 comments on commit b7c9bb6

Please sign in to comment.