cmake_minimum_required(VERSION 3.5) project(photon C ASM-ATT) macro(subdirlist result curdir) file(GLOB children RELATIVE ${curdir} ${curdir}/*) set(dirlist "") foreach (child ${children}) if (IS_DIRECTORY ${curdir}/${child}) list(APPEND dirlist ${child}) endif() endforeach() set(${result} ${dirlist}) endmacro() subdirlist(machine_dirs ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/machine) if (NOT PLATFORM) set(PLATFORM "linux") endif () if (NOT MACHINE) set(supported_machines "") foreach (dir ${machine_dirs}) set(supported_machines "${supported_machines}${dir} ") endforeach (dir) message(FATAL_ERROR "No machine architecture specified.\n" " Supported machines: ${supported_machines}\n" " Ex. `cmake -DMACHINE=x86_64 `") endif () if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}) message(FATAL_ERROR "Unsupported platform: ${PLATFORM}") endif () message(STATUS "Target platform: ${PLATFORM}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ffreestanding -nostdlib -lgcc") set(photon_libc_sources "") set(generic_dirs string) foreach (dir ${generic_dirs}) file(GLOB dir_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/${dir}/*.c) set(photon_libc_sources ${photon_libc_sources} ${dir_sources}) endforeach (dir) file(GLOB_RECURSE photon_libc_headers ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include/*.h) file(GLOB photon_libc_crt ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/crt*.s) add_library(crt OBJECT ${photon_libc_crt}) file(GLOB platform_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/*.s) foreach (crt_source ${photon_libc_crt}) list(REMOVE_ITEM platform_sources ${crt_source}) endforeach (crt_source) file(GLOB platform_headers ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/machine/${MACHINE}/*.h) set(photon_libc_sources ${photon_libc_sources} ${platform_sources}) set(photon_libc_headers ${photon_libc_headers} ${platform_headers}) add_library(c STATIC ${photon_libc_sources} ${photon_libc_headers}) target_compile_options(c PRIVATE -ffreestanding -nostdlib -nostdinc) target_link_libraries(c crt) target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include) add_custom_target(sysroot DEPENDS ${photon_libc_headers} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/create-sysroot.sh ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${PLATFORM} ${MACHINE}) add_dependencies(c sysroot) add_subdirectory(tests)