diff --git a/CMakeLists.txt b/CMakeLists.txt index ffb422e..d2211d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.5) -project(photon) +project(photon C ASM-ATT) macro(subdirlist result curdir) file(GLOB children RELATIVE ${curdir} ${curdir}/*) @@ -36,18 +36,37 @@ 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 include) +set(generic_dirs string) foreach (dir ${generic_dirs}) - file(GLOB_RECURSE dir_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/${dir}/*.c) + 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 platform_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/sys/${PLATFORM}) +file(GLOB_RECURSE photon_libc_includes ${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}) -add_library(c STATIC ${photon_libc_sources}) +add_library(c STATIC ${photon_libc_sources} ${photon_libc_includes}) 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_subdirectory(tests) diff --git a/photon/libc/include/machine/_stdint.h b/photon/libc/include/machine/_stdint.h index 7dcf5b1..4a510ed 100644 --- a/photon/libc/include/machine/_stdint.h +++ b/photon/libc/include/machine/_stdint.h @@ -14,9 +14,9 @@ #define __have_long32 1 #endif -#ifdef __cplusplus +#if defined(__cplusplus) extern "C" { -#endif +#endif /* defined(__cplusplus) */ #ifdef __INT8_TYPE__ typedef __INT8_TYPE__ __int8_t; @@ -236,8 +236,8 @@ typedef __uint64_t __uint_fast64_t; #undef __EXP -#ifdef __cplusplus +#if defined(__cplusplus) } -#endif +#endif /* defined(__cplusplus) */ #endif /* _MACHINE__STDINT_H */ diff --git a/photon/libc/include/stddef.h b/photon/libc/include/stddef.h index 87509ea..270be52 100644 --- a/photon/libc/include/stddef.h +++ b/photon/libc/include/stddef.h @@ -8,7 +8,7 @@ #if defined(__cplusplus) extern "C" { -#endif +#endif /* defined(__cplusplus) */ typedef __intptr_t ptrdiff_t; typedef __uintptr_t size_t; @@ -16,6 +16,6 @@ typedef int wchar_t; #if defined(__cplusplus) } -#endif +#endif /* defined(__cplusplus) */ -#endif +#endif /* STDDEF_H_ */ diff --git a/photon/libc/include/stdint.h b/photon/libc/include/stdint.h index 361107c..117785c 100644 --- a/photon/libc/include/stdint.h +++ b/photon/libc/include/stdint.h @@ -50,4 +50,4 @@ typedef __uintptr_t uintptr_t; typedef -#endif +#endif /* STDINT_H_ */ diff --git a/photon/libc/machine/i386/types.h b/photon/libc/machine/i386/types.h deleted file mode 100644 index e69de29..0000000 diff --git a/photon/libc/sys/linux/machine/x86_64/crt0.s b/photon/libc/sys/linux/machine/x86_64/crt0.s new file mode 100644 index 0000000..36292af --- /dev/null +++ b/photon/libc/sys/linux/machine/x86_64/crt0.s @@ -0,0 +1,15 @@ +.global _start +.type _start, @function + +.extern main +.type main, @function + +_start: + pop %rbp + pop %rdi + mov %rsp, %rsi + andq $-16, %rsp + call main + movq %rax, %rdi + movq $60, %rax + syscall diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..2402243 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,8 @@ +file(GLOB test_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.c) + +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_link_libraries(${test_name} c) +endforeach (test) diff --git a/tests/start.c b/tests/start.c new file mode 100644 index 0000000..c006f23 --- /dev/null +++ b/tests/start.c @@ -0,0 +1,4 @@ +int main(int argc, char **argv) +{ + return 42; +}