From 08df7d7a283354ed8ef96d59947176f4faff651e Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 7 Jul 2020 18:53:54 +0100 Subject: [PATCH] Photon is now built as a framework --- CMakeLists.txt | 35 ++++--- cmake/Bundles.cmake | 139 +++++++++++++++++++++++++++ photon/libc/include/stdio.h | 2 +- photon/libc/stdio/fputc.c | 2 +- photon/libc/sys/linux/config.cmake | 0 photon/libc/sys/magenta/config.cmake | 2 +- scripts/add-framework-crt.sh | 11 +++ 7 files changed, 176 insertions(+), 15 deletions(-) create mode 100644 cmake/Bundles.cmake create mode 100644 photon/libc/sys/linux/config.cmake create mode 100755 scripts/add-framework-crt.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 825deb9..63db8cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.5) -#project(photon C ASM-ATT) +# project(photon C ASM-ATT) macro(subdirlist result curdir) file(GLOB children RELATIVE ${curdir} ${curdir}/*) @@ -13,6 +13,7 @@ macro(subdirlist result curdir) endmacro() include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Bundles.cmake) platform_config(${PHOTON_TARGET}) message(STATUS "Target: ${machine}-${platform}") @@ -73,21 +74,21 @@ file(GLOB platform_headers set(photon_libc_sources ${photon_libc_sources} ${malloc_sources} ${platform_sources}) set(photon_libc_headers ${photon_libc_headers} ${platform_headers}) -if (PHOTON_STATIC EQUAL 1) - add_library(c STATIC ${photon_libc_sources} ${photon_libc_headers}) -else () - add_library(c SHARED ${photon_libc_sources} ${photon_libc_headers}) -endif () +add_framework(Photon STATIC + BUNDLE_ID net.doorstuck.photon + SOURCES ${photon_libc_sources} ${photon_libc_headers} + HEADERS ${CMAKE_CURRENT_BINARY_DIR}/sysroot/usr/include/*) -target_compile_options(c PRIVATE -ffreestanding -nostdlib) -target_link_libraries(c crt ${photon_platform_libs}) +framework_compile_options(Photon -ffreestanding -nostdlib) +framework_link_libraries(Photon ${photon_platform_libs}) +framework_link_frameworks(Photon ${photon_platform_frameworks}) -target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include +framework_include_directories(Photon ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/include ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/ ${CMAKE_CURRENT_BINARY_DIR}/sysroot/usr/include) foreach (platform_include_dir ${photon_platform_extra_include_dirs}) - target_include_directories(c PRIVATE + framework_include_directories(Photon ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/${platform_include_dir}) endforeach (platform_include_dir) @@ -98,10 +99,20 @@ add_custom_target(local-root ${CMAKE_CURRENT_BINARY_DIR} ${platform} ${machine}) -add_dependencies(c local-root) +framework_add_dependencies(Photon local-root) + +add_custom_command(TARGET Photon POST_BUILD + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/add-framework-crt.sh + ${CMAKE_CURRENT_BINARY_DIR}/Photon.framework + ${CMAKE_CURRENT_BINARY_DIR}) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_link_libraries(c gcc) + framework_link_libraries(Photon gcc) +endif () + +if (TARGET early-sysroot) + # We are being built as part of asbestOS, make sure the sysroot is there. + framework_add_dependencies(Photon early-sysroot) endif () if (PHOTON_TESTS EQUAL 1) diff --git a/cmake/Bundles.cmake b/cmake/Bundles.cmake new file mode 100644 index 0000000..dbdf5db --- /dev/null +++ b/cmake/Bundles.cmake @@ -0,0 +1,139 @@ +find_program(BUNDLE_PROGRAM "bundle") +if (BUNDLE_PROGRAM STREQUAL BUNDLE_PROGRAM-NOTFOUND) + message(WARNING "Couldn't find Bundle generator. " + "Please install bundle from https://gitalb.com/doorstuck/magenta/bundle.git") +endif () + +function (add_framework name) + set(options STATIC SHARED) + set(one_value_args BUNDLE_ID) + set(multi_value_args HEADER_DIRS HEADERS SOURCES) + cmake_parse_arguments(ARG "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + if (ARG_HEADER_DIRS STREQUAL "") + message(FATAL_ERROR "add_framework: No header directories specified for ${name}") + endif () + + if (ARG_SOURCES STREQUAL "") + message(FATAL_ERROR "add_framework: No source files specified for ${name}") + endif () + + if (ARG_BUNDLE_ID STREQUAL "") + message(FATAL_ERROR "add_framework: No bundle ID specified for ${name}") + endif () + + string(TOLOWER ${name} lib_name) + + if (NOT ARG_STATIC AND NOT ARG_SHARED) + message(FATAL_ERROR "add_framework: Neither STATIC nor SHARED specified for ${name}") + endif () + + add_custom_target(${name} ALL) + + if (ARG_STATIC) + add_library(${lib_name}_s STATIC ${ARG_SOURCES}) + add_dependencies(${name} ${lib_name}_s) + endif () + + if (ARG_SHARED) + add_library(${lib_name} SHARED ${ARG_SOURCES}) + add_dependencies(${name} ${lib_name}) + endif () + + set(header_dirs_arg "") + foreach (header_dir ${ARG_HEADER_DIRS}) + set(header_dirs_arg ${header_dirs_arg} --header-dir \"${header_dir}\") + endforeach (header_dir) + + set(headers_arg "") + foreach (header ${ARG_HEADERS}) + set(headers_arg ${headers_arg} --header \"${header}\") + endforeach (header) + + set(binary_args "") + if (ARG_STATIC) + set(binary_args ${binary_args} --static-binary \"${CMAKE_CURRENT_BINARY_DIR}/lib${lib_name}_s.a\") + endif () + + if (ARG_SHARED) + set(binary_args ${binary_args} --shared-binary \"${CMAKE_CURRENT_BINARY_DIR}/lib${lib_name}.so\") + endif () + + string(ASCII 27 esc) + string(ASCII 10 lf) + string(ASCII 13 cr) + + add_custom_command(TARGET ${name} + POST_BUILD + COMMENT "[ -- ] Building framework bundle ${name}.framework" + COMMAND rm -rf ${name}.framework + COMMAND ${BUNDLE_PROGRAM} create-framework + -o ${name}.framework + -i ${ARG_BUNDLE_ID} + ${headers_arg} + ${header_dirs_arg} + ${binary_args} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +endfunction (add_framework) + +function (framework_compile_options name) + string(TOLOWER ${name} lib_name) + + if (TARGET ${lib_name}) + target_compile_options(${lib_name} PRIVATE ${ARGN}) + endif () + + if (TARGET ${lib_name}_s) + target_compile_options(${lib_name}_s PRIVATE ${ARGN}) + endif () +endfunction (framework_compile_options) + +function (framework_link_libraries name) + string(TOLOWER ${name} lib_name) + + if (TARGET ${lib_name}) + target_link_libraries(${lib_name} ${ARGN}) + endif () + + if (TARGET ${lib_name}_s) + target_link_libraries(${lib_name}_s ${ARGN}) + endif () +endfunction (framework_link_libraries) + +function (framework_include_directories name) + string(TOLOWER ${name} lib_name) + + if (TARGET ${lib_name}) + target_include_directories(${lib_name} PRIVATE ${ARGN}) + endif () + + if (TARGET ${lib_name}_s) + target_include_directories(${lib_name}_s PRIVATE ${ARGN}) + endif () +endfunction (framework_include_directories) + +function (framework_link_frameworks name) + string(TOLOWER ${name} lib_name) + + foreach (framework ${ARGN}) + if (TARGET ${lib_name}) + target_compile_options(${lib_name} PRIVATE -framework ${framework}) + endif () + + if (TARGET ${lib_name}_s) + target_compile_options(${lib_name}_s PRIVATE -framework ${framework}) + endif () + endforeach (framework) +endfunction (framework_link_frameworks) + +function (framework_add_dependencies name) + string(TOLOWER ${name} lib_name) + + if (TARGET ${lib_name}) + add_dependencies(${lib_name} ${ARGN}) + endif () + + if (TARGET ${lib_name}_s) + add_dependencies(${lib_name}_s ${ARGN}) + endif () +endfunction (framework_add_dependencies) diff --git a/photon/libc/include/stdio.h b/photon/libc/include/stdio.h index f58b5f1..ef062c0 100644 --- a/photon/libc/include/stdio.h +++ b/photon/libc/include/stdio.h @@ -35,7 +35,7 @@ extern int fflush(FILE *fp); extern int fileno(FILE *fp); extern int fputs(const char *str, FILE *fp); -extern int fputc(char c, FILE *fp); +extern int fputc(int c, FILE *fp); extern int printf(const char *restrict format, ...); extern int fprintf(FILE *fp, const char *restrict format, ...); diff --git a/photon/libc/stdio/fputc.c b/photon/libc/stdio/fputc.c index 8b472dd..bac9bbd 100644 --- a/photon/libc/stdio/fputc.c +++ b/photon/libc/stdio/fputc.c @@ -1,7 +1,7 @@ #include #include <__fio.h> -int fputc(char c, FILE *fp) +int fputc(int c, FILE *fp) { __fio_write(fp, &c, 1); if (__fio_error(fp)) { diff --git a/photon/libc/sys/linux/config.cmake b/photon/libc/sys/linux/config.cmake new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/sys/magenta/config.cmake b/photon/libc/sys/magenta/config.cmake index f6289f8..1273266 100644 --- a/photon/libc/sys/magenta/config.cmake +++ b/photon/libc/sys/magenta/config.cmake @@ -2,4 +2,4 @@ set(photon_platform_extra_source_dirs libmagenta/libmagenta) set(photon_platform_extra_include_dirs libmagenta/libmagenta libmagenta/libmagenta/arch/${machine}) -set(photon_platform_libs magenta) +set(photon_platform_frameworks Magenta) diff --git a/scripts/add-framework-crt.sh b/scripts/add-framework-crt.sh new file mode 100755 index 0000000..901e466 --- /dev/null +++ b/scripts/add-framework-crt.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +framework_path=$1 +binary_dir=$2 + +crt_files=$(find $binary_dir -name "crt*.s.o") + +for crt_file in $crt_files; do + out="$framework_path/Binary/$(basename $crt_file .s.o).o" + cp -f $crt_file $out +done