Lots of work on the build system

This commit is contained in:
Max Wash
2020-03-31 16:21:07 +01:00
parent 28256512c9
commit 76d1753ab5
11 changed files with 130 additions and 12 deletions

View File

@@ -33,7 +33,6 @@ if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM})
message(FATAL_ERROR "Unsupported platform: ${PLATFORM}") message(FATAL_ERROR "Unsupported platform: ${PLATFORM}")
endif () endif ()
message(STATUS "Target platform: ${PLATFORM}") message(STATUS "Target platform: ${PLATFORM}")
set(CMAKE_EXE_LINKER_FLAGS set(CMAKE_EXE_LINKER_FLAGS
@@ -47,26 +46,42 @@ foreach (dir ${generic_dirs})
set(photon_libc_sources ${photon_libc_sources} ${dir_sources}) set(photon_libc_sources ${photon_libc_sources} ${dir_sources})
endforeach (dir) 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 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})
message(STATUS "CRT sources: ${photon_libc_crt}")
file(GLOB platform_sources file(GLOB platform_sources
${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}/machine/${MACHINE}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${PLATFORM}/machine/${MACHINE}/*.s)
${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})
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_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)
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) add_subdirectory(tests)

View File

@@ -48,6 +48,4 @@ typedef __uintmax_t uintmax_t;
typedef __intptr_t intptr_t; typedef __intptr_t intptr_t;
typedef __uintptr_t uintptr_t; typedef __uintptr_t uintptr_t;
typedef
#endif /* STDINT_H_ */ #endif /* STDINT_H_ */

View File

@@ -0,0 +1,30 @@
#ifndef STDIO_H_
#define STDIO_H_
#include <sys/_fconst.h>
#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

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}

View File

@@ -1,6 +1,9 @@
.global _start .global _start
.type _start, @function .type _start, @function
.extern __fio_init
.type __fio_init, @function
.extern main .extern main
.type main, @function .type main, @function
@@ -9,6 +12,7 @@ _start:
pop %rdi pop %rdi
mov %rsp, %rsi mov %rsp, %rsi
andq $-16, %rsp andq $-16, %rsp
call __fio_init
call main call main
movq %rax, %rdi movq %rax, %rdi
movq $60, %rax movq $60, %rax

20
scripts/create-sysroot.sh Executable file
View File

@@ -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

View File

@@ -4,5 +4,7 @@ foreach (test ${test_sources})
get_filename_component(test_name ${test} NAME_WE) get_filename_component(test_name ${test} NAME_WE)
add_executable(${test_name} ${test} $<TARGET_OBJECTS:crt>) add_executable(${test_name} ${test} $<TARGET_OBJECTS:crt>)
target_compile_options(${test_name} PRIVATE -ffreestanding -nostdlib -nostdinc) 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) target_link_libraries(${test_name} c)
endforeach (test) endforeach (test)

View File

@@ -1,3 +1,6 @@
#include <stdio.h>
#include <stdint.h>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
return 42; return 42;