New platform selection system in build system

This commit is contained in:
Max Wash
2020-04-01 12:41:26 +01:00
parent 1e237adbc0
commit bee0a8ff9f
3 changed files with 55 additions and 29 deletions

View File

@@ -12,28 +12,10 @@ macro(subdirlist result curdir)
set(${result} ${dirlist}) set(${result} ${dirlist})
endmacro() endmacro()
subdirlist(machine_dirs ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/machine) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform.cmake)
platform_config(${TARGET})
if (NOT PLATFORM) message(STATUS "Target: ${machine}-${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 <dir>`")
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 set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -ffreestanding -nostdlib -lgcc") "${CMAKE_EXE_LINKER_FLAGS} -ffreestanding -nostdlib -lgcc")
@@ -49,22 +31,22 @@ endforeach (dir)
file(GLOB_RECURSE photon_libc_headers ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include/*.h) file(GLOB_RECURSE photon_libc_headers ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include/*.h)
file(GLOB photon_libc_crt file(GLOB photon_libc_crt
${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/crt*.s) ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/machine/${machine}/crt*.s)
add_library(crt OBJECT ${photon_libc_crt}) add_library(crt OBJECT ${photon_libc_crt})
file(GLOB platform_sources file(GLOB platform_sources
${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/*.c ${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}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/*.s) ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/machine/${machine}/*.s)
foreach (crt_source ${photon_libc_crt}) foreach (crt_source ${photon_libc_crt})
list(REMOVE_ITEM platform_sources ${crt_source}) list(REMOVE_ITEM platform_sources ${crt_source})
endforeach (crt_source) endforeach (crt_source)
file(GLOB platform_headers file(GLOB platform_headers
${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${platform}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/machine/${MACHINE}/*.h) ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${platform}/machine/${machine}/*.h)
set(photon_libc_sources ${photon_libc_sources} ${platform_sources}) set(photon_libc_sources ${photon_libc_sources} ${platform_sources})
set(photon_libc_headers ${photon_libc_headers} ${platform_headers}) set(photon_libc_headers ${photon_libc_headers} ${platform_headers})
@@ -74,14 +56,14 @@ target_compile_options(c PRIVATE -ffreestanding -nostdlib -nostdinc)
target_link_libraries(c crt) target_link_libraries(c crt)
target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include) target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include)
target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/) target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/)
add_custom_target(sysroot add_custom_target(sysroot
DEPENDS ${photon_libc_headers} DEPENDS ${photon_libc_headers}
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/create-sysroot.sh COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/create-sysroot.sh
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
${PLATFORM} ${MACHINE}) ${platform} ${machine})
add_dependencies(c sysroot) add_dependencies(c sysroot)

44
cmake/platform.cmake Normal file
View File

@@ -0,0 +1,44 @@
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()
function (platform_config)
set(available_platforms "")
set(available_platforms_list "")
subdirlist(platforms ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys)
foreach (platform ${platforms})
subdirlist(machines ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/machine)
foreach (machine ${machines})
set(available_platforms "${available_platforms}${machine}-${platform} ")
list(APPEND available_platforms_list ${machine}-${platform})
endforeach (machine)
endforeach (platform)
if (${ARGC} LESS 1)
message(FATAL_ERROR "No target specified!\n"
"Available targets: ${available_platforms}")
endif ()
set(target ${ARGV0})
list(FIND available_platforms_list ${target} target_idx)
if (${target_idx} EQUAL -1)
message(FATAL_ERROR "${target} is not a valid target!\n"
"Available targets: ${available_platforms}")
endif ()
string(REPLACE "-" ";" target_parts ${target})
list(GET target_parts 1 platform)
list(GET target_parts 0 machine)
set(machine ${machine} PARENT_SCOPE)
set(platform ${platform} PARENT_SCOPE)
endfunction (platform_config)