diff --git a/CMakeLists.txt b/CMakeLists.txt index d2211d6..cbc3219 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,6 @@ 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 @@ -47,26 +46,42 @@ foreach (dir ${generic_dirs}) set(photon_libc_sources ${photon_libc_sources} ${dir_sources}) endforeach (dir) -file(GLOB_RECURSE photon_libc_includes ${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 ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/crt*.s) add_library(crt OBJECT ${photon_libc_crt}) -message(STATUS "CRT sources: ${photon_libc_crt}") file(GLOB platform_sources - ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/*.c - ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/*.h - ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/machine/${MACHINE}/*.c - ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/machine/${MACHINE}/*.h - ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/machine/${MACHINE}/*.s) -set(photon_libc_sources ${photon_libc_sources} ${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) -add_library(c STATIC ${photon_libc_sources} ${photon_libc_includes}) +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) diff --git a/photon/libc/include/stdint.h b/photon/libc/include/stdint.h index 117785c..cd1252d 100644 --- a/photon/libc/include/stdint.h +++ b/photon/libc/include/stdint.h @@ -48,6 +48,4 @@ typedef __uintmax_t uintmax_t; typedef __intptr_t intptr_t; typedef __uintptr_t uintptr_t; -typedef - #endif /* STDINT_H_ */ diff --git a/photon/libc/include/stdio.h b/photon/libc/include/stdio.h new file mode 100644 index 0000000..0b8c7d3 --- /dev/null +++ b/photon/libc/include/stdio.h @@ -0,0 +1,30 @@ +#ifndef STDIO_H_ +#define STDIO_H_ + +#include + +#if defined(__FILENAME_MAX) +#define FILENAME_MAX __FILENAME_MAX +#else +#define FILENAME_MAX 1024 +#endif + +#define SEEK_SET __SEEK_SET +#define SEEK_CUR __SEEK_CUR +#define SEEK_END __SEEK_END + +#if defined(__cplusplus) +extern "C" { +#endif + +typedef struct __io_file FILE; + +extern FILE *stdin; +extern FILE *stdout; +extern FILE *stderr; + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/photon/libc/machine/x86_64/types.h b/photon/libc/machine/x86_64/types.h deleted file mode 100644 index e69de29..0000000 diff --git a/photon/libc/sys/linux/__fio.h b/photon/libc/sys/linux/__fio.h new file mode 100644 index 0000000..362c0a8 --- /dev/null +++ b/photon/libc/sys/linux/__fio.h @@ -0,0 +1,20 @@ +#ifndef SYS_LINUX___FIO_H_ +#define SYS_LINUX___FIO_H_ + +#define __FIO_BUFSZ 512 + +#if defined(__cplusplus) +extern "C" { +#endif + +struct __io_file { + char buf[__FIO_BUFSZ]; + unsigned int buf_idx; + unsigned int fd; +}; + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/photon/libc/sys/linux/_fconst.h b/photon/libc/sys/linux/_fconst.h new file mode 100644 index 0000000..9696e9c --- /dev/null +++ b/photon/libc/sys/linux/_fconst.h @@ -0,0 +1,10 @@ +#ifndef SYS_LINUX__FCONST_H_ +#define SYS_LINUX__FCONST_H_ + +#define __FILENAME_MAX 1024 + +#define __SEEK_SET 0 +#define __SEEK_CUR 1 +#define __SEEK_END 2 + +#endif diff --git a/photon/libc/sys/linux/fio.c b/photon/libc/sys/linux/fio.c new file mode 100644 index 0000000..150a651 --- /dev/null +++ b/photon/libc/sys/linux/fio.c @@ -0,0 +1,16 @@ +#include "__fio.h" + +struct __io_file __stdin = {}; +struct __io_file __stdout = {}; +struct __io_file __stderr = {}; + +struct __io_file *stdin = &__stdin; +struct __io_file *stdout = &__stdout; +struct __io_file *stderr = &__stderr; + +void __fio_init() +{ + __stdin.fd = 0; + __stdout.fd = 1; + __stderr.fd = 2; +} diff --git a/photon/libc/sys/linux/machine/x86_64/crt0.s b/photon/libc/sys/linux/machine/x86_64/crt0.s index 36292af..6941741 100644 --- a/photon/libc/sys/linux/machine/x86_64/crt0.s +++ b/photon/libc/sys/linux/machine/x86_64/crt0.s @@ -1,6 +1,9 @@ .global _start .type _start, @function +.extern __fio_init +.type __fio_init, @function + .extern main .type main, @function @@ -9,6 +12,7 @@ _start: pop %rdi mov %rsp, %rsi andq $-16, %rsp + call __fio_init call main movq %rax, %rdi movq $60, %rax diff --git a/scripts/create-sysroot.sh b/scripts/create-sysroot.sh new file mode 100755 index 0000000..b6b6626 --- /dev/null +++ b/scripts/create-sysroot.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +source_dir=$1 +build_dir=$2 +platform=$3 +machine=$4 + +echo "Building $machine-$platform sysroot" +mkdir -p $build_dir/sysroot/usr/include/{sys,machine} + +cp $source_dir/photon/libc/include/*.h $build_dir/sysroot/usr/include + +platform_headers=$(find $source_dir/photon/libc/sys/$platform -type f \( -iname "*.h" ! -iname "__*" \)) +cp -t $build_dir/sysroot/usr/include/sys $platform_headers + +machine_arch_headers=$(find $source_dir/photon/libc/machine/$machine/ \ + -type f \( -iname "*.h" ! -iname "__*" \)) +machine_generic_headers=$(find $source_dir/photon/libc/include/machine/ \ + -type f \( -iname "*.h" ! -iname "__*" \)) +cp -t $build_dir/sysroot/usr/include/machine $machine_generic_headers $machine_arch_headers diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2402243..d094ca0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,5 +4,7 @@ foreach (test ${test_sources}) get_filename_component(test_name ${test} NAME_WE) add_executable(${test_name} ${test} $) target_compile_options(${test_name} PRIVATE -ffreestanding -nostdlib -nostdinc) + target_include_directories(${test_name} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR}/../sysroot/usr/include) target_link_libraries(${test_name} c) endforeach (test) diff --git a/tests/start.c b/tests/start.c index c006f23..b9b1667 100644 --- a/tests/start.c +++ b/tests/start.c @@ -1,3 +1,6 @@ +#include +#include + int main(int argc, char **argv) { return 42;