Initial source push!

This commit is contained in:
Max 2023-07-15 19:52:58 +02:00
parent 3a775a94a6
commit 21f40e3083
26 changed files with 552 additions and 0 deletions

82
.gitignore vendored Executable file
View File

@ -0,0 +1,82 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# CMAKE
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
cmake_install.cmake
CTestTestfile.cmake
# Make
Makefile
# Build
Build/
build/
BUILD/
bin/
BIN/
lib/
LIB/
out/
# IDE
.vs/
.vscode/
.idea/
.kdev/
.kdev4/
*.kdev
*.kdev4

60
CMakeLists.txt Executable file
View File

@ -0,0 +1,60 @@
cmake_minimum_required(VERSION 3.5)
project("Based" LANGUAGES C ASM)
# =============================================
# Options
# =============================================
#unset(DOCUMENTATION CACHE)
#unset(INTERNAL_DOC, CACHE)
option(DOCUMENTATION "Enable to generate ducumentation." OFF)
option(INTERNAL_DOC "Enable to generate internal & public documentaion. Disable to only generate public documentation" OFF)
# =============================================
# Policies
# =============================================
cmake_policy(SET CMP0076 NEW)
# =============================================
# Compiler
# =============================================
if(CMAKE_C_COMPILER_ID STREQUAL "GNU") # GNUCC
# Add linker options.
add_link_options(-nostdlib -nodefaultlibs -nostdinc -lgcc) # -z noexecstack
add_compile_options(
-Wall -Wextra -Wshadow -Wfloat-equal -Wpointer-arith -fPIC -fno-builtin
"$<$<CONFIG:RELEASE>:-s;-O3;-DNDEBUG>"
"$<$<CONFIG:DEBUG>:-O0;-g>"
"$<$<CONFIG:RELWITHDEBINFO>:-O2;-g;-DNDEBUG>")
elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") # MSVC
#We use a stack of 1MiB, it is cleaner to implement the chkstk function to check if we need to init more pages.
add_link_options(/NODEFAULTLIB /STACK:0x100000,0x100000)
add_compile_options(
/DWIN32 /D_WINDOWS /W3 /Zl /GS- /Gs9999999 /c
"$<$<CONFIG:DEBUG>:/MDd;/Zi;/Ob0;/Od>"
)
endif()
# =============================================
# Directories
# =============================================
set(PROJ_BUILD_DIR ${CMAKE_BINARY_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJ_BUILD_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJ_BUILD_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJ_BUILD_DIR}/bin)
set(PROJ_REL_BUILD_DIR ${CMAKE_BINARY_DIR}/release)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJ_REL_BUILD_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJ_REL_BUILD_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJ_REL_BUILD_DIR}/bin)
set(PROJ_DEB_BUILD_DIR ${CMAKE_BINARY_DIR}/debug)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJ_DEB_BUILD_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJ_DEB_BUILD_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJ_DEB_BUILD_DIR}/bin)
if (DOCUMENTATION)
add_subdirectory(doc)
endif(DOCUMENTATION)
add_subdirectory(src)

45
doc/CMakeLists.txt Executable file
View File

@ -0,0 +1,45 @@
#Cmake: Documentation
find_package(Doxygen REQUIRED doxygen)
find_program(HAS_GRAPHVIZ NAMES dot)
if(HAS_GRAPHVIZ)
message("Graphviz found!")
set(DOXYGEN_HAVE_DOT YES) # Needs graphviz
endif()
set(DOXYGEN_CREATE_SUBDIRS YES)
set(DOXYGEN_CREATE_SUBDIRS_LEVEL 8)
set(DOXYGEN_HTML_COLORSTYLE DARK)
set(DOXYGEN_FULL_PATH_NAMES NO)
set(DOXYGEN_EXTRACT_ALL YES)
set(DOXYGEN_EXTRACT_PRIVATE YES)
set(DOXYGEN_EXTRACT_STATIC YES)
set(DOXYGEN_CALL_GRAPH YES)
set(DOXYGEN_CALLER_GRAPH YES)
set(DOXYGEN_DISABLE_INDEX YES)
set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES)
set(DOXYGEN_ALPHABETICAL_INDEX NO)
set(DOXYGEN_EXCLUDE build)
set(DOXYGEN_FILE_PATTERNS *.c *.h)
set(DOXYGEN_GENERATE_TREEVIEW YES)
set(DOXYGEN_QUIET YES)
set(DOXYGEN_RECURSIVE YES)
set(DOXYGEN_REFERENCED_BY_RELATION YES)
set(DOXYGEN_REFERENCES_RELATION YES)
set(DOXYGEN_SORT_BY_SCOPE_NAME YES)
set(DOXYGEN_SORT_MEMBER_DOCS NO)
set(DOXYGEN_SOURCE_BROWSER YES)
set(DOXYGEN_STRIP_CODE_COMMENTS NO)
if(INTERNAL_DOC)
set(DOC_PATHS "${CMAKE_SOURCE_DIR}/src")
set(DOXYGEN_PROJECT_NAME "${CMAKE_PROJECT_NAME} Internal")
message("Internal doc enabled!")
else()
set(DOC_PATHS "${CMAKE_SOURCE_DIR}/src/rtbase/include")
message("Public doc enabled!")
endif(INTERNAL_DOC)
doxygen_add_docs(doxygen ${DOC_PATHS}
ALL
COMMENT "Generating HTML documentation")

5
src/CMakeLists.txt Executable file
View File

@ -0,0 +1,5 @@
# src directory
add_subdirectory(based)
add_subdirectory(runtime)
add_subdirectory(cli)

16
src/based/CMakeLists.txt Executable file
View File

@ -0,0 +1,16 @@
# Source based library
add_library(based SHARED "base.c")
target_compile_features(based PRIVATE c_std_99)
target_include_directories(based
PUBLIC
${CMAKE_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
${CMAKE_SOURCE_DIR}/src)
add_subdirectory(include)
add_subdirectory(system)

8
src/based/base.c Executable file
View File

@ -0,0 +1,8 @@
#include "base.h"
// Test export function, else there will be no symbols exported on the build.
EXPORT int test()
{
return 69;
}

View File

@ -0,0 +1,6 @@
# based => include
target_sources(based PUBLIC
"base.h")
add_subdirectory(env)

39
src/based/include/base.h Executable file
View File

@ -0,0 +1,39 @@
/**
* @file base_defs.h
* @brief The library general header.
* @version 0.1
* @date 2023-01-11
* @details Contains utilities like memory management, evironment and more.
*/
#ifndef BASE_H
#define BASE_H
#include "env/envb.h"
// ========================================
// Definitions
// ========================================
/// Exit on success.
#define EXIT_OK 0
/// Exit on fail.
#define EXIT_FAIL -1
/// Exit on error.
#define EXIT_ERROR 1
#if !defined(bool) || !defined(BOOL)
#define bool uint8
#define true 1
#define false 0
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif // NULL
#define INVALID_PTR ((void*)-1)
#define NULL_BYTE_PTR ((BYTE*)0)
#define INVALID_BYTE_PTR ((BYTE*)-1)
#define NULL_BYTE '\0'
#define IGNORE_ARG(x) (void)x;
#endif //BASE_H

3
src/based/include/env/CMakeLists.txt vendored Executable file
View File

@ -0,0 +1,3 @@
# based => include => env
target_sources(based PRIVATE "envb.h")

94
src/based/include/env/envb.h vendored Executable file
View File

@ -0,0 +1,94 @@
/**
* @file envb.h
* @brief Environment base header.
* @version 0.1
* @date 2023-01-11
*/
#ifndef ENVB_H
#define ENVB_H
// ========================================
// Linux
// ========================================
#ifdef __gnu_linux__
#define OS_LINUX
#define EXPORT __attribute__((visibility("default")))
#define CDECL_ATT __attribute__((cdecl))
#define NO_RETURN_ATT __attribute__((noreturn))
#ifdef __x86_64__
#if defined(__i386__) || defined(_M_IX86)
#define OS_ENV 32
#else // defined(__i386__) || defined(_M_IX86)
#define OS_ENV 64
#endif // defined(__i386__) || defined(_M_IX86)
#endif // __x86_64__
#endif // __gnu_linux__
// ========================================
// Windows
// ========================================
#ifdef _WIN32
#define OS_WINDOWS 1
#define EXPORT __declspec(dllexport)
#ifdef _WIN64 // _WIN64
#define OS_ENV 64
#else // x32
#define OS_ENV 32
#endif // _WIN64
#endif // _WIN32
// ===================================================================
//
// Lib types
//
// ===================================================================
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
#if OS_ENV == 64
typedef uint64 uintptr;
#else
typedef uint32 uintptr;
#endif
/**
* @brief Typedef for process identifier.
*/
typedef int32 p_id;
/**
* @brief Typedef for wide char.
*/
typedef uint16 w_char;
/**
* @brief Typedef for byte.
*/
typedef uint8 BYTE;
/**
* @brief Typedef for bit field.
*/
typedef uint32 bit_field;
/**
* @brief Typedef for offset.
*/
typedef long int off_set;
/**
* @brief Typedef for usize.
*/
typedef long unsigned int usize;
/**
* @brief Typedef for ssize.
*/
typedef long signed int ssize;
#endif //ENVB_H

10
src/based/system/CMakeLists.txt Executable file
View File

@ -0,0 +1,10 @@
# based => system
target_sources(based PRIVATE "bootstrap.c")
if(UNIX)
add_subdirectory(unix)
endif(UNIX)
if(WIN32)
add_subdirectory(win32)
endif(WIN32)

24
src/based/system/bootstrap.c Executable file
View File

@ -0,0 +1,24 @@
#include "base.h"
//TODO(Implement): Handle the boostrapping of the application.
enum bootstrap_sys
{
sys_unix = 0,
sys_win32 = 1
};
/// @brief Executes the bootstrapping process.
int base_bootstrap(enum bootstrap_sys system)
{
switch (system)
{
case sys_unix:
break;
case sys_win32:
break;
default:
return -1;
break;
}
return 0;
}

View File

@ -0,0 +1,2 @@
# based => system => unix

View File

@ -0,0 +1,3 @@
# based => system => win32
target_sources(based PRIVATE "win32sym.c")

View File

@ -0,0 +1,21 @@
#include "base.h"
#include "windows.h"
BOOL WINAPI _DllMainCRTStartup(HINSTANCE const instance, DWORD const reason, LPVOID const reserved) // => DllMain
{
(void)instance;
(void)reason;
(void)reserved;
//return DllMain(instance, reason, reserved);
return TRUE;
}
BOOL WINAPI _RTC_InitBase()
{
return TRUE;
}
BOOL WINAPI _RTC_Shutdown()
{
return TRUE;
}

5
src/cli/CMakeLists.txt Executable file
View File

@ -0,0 +1,5 @@
# cli project
add_executable(basedcli "main.c")
target_link_libraries(basedcli runtime)

6
src/cli/main.c Executable file
View File

@ -0,0 +1,6 @@
#include "base.h"
int main(int argc, char* argv[], char* envp[])
{
return 0;
}

15
src/runtime/CMakeLists.txt Executable file
View File

@ -0,0 +1,15 @@
# runtime library
add_library(runtime STATIC
"rtinternal.h"
"runtime.c")
target_link_libraries(runtime PUBLIC based)
target_include_directories(runtime
PUBLIC
${CMAKE_SOURCE_DIR}/include
PRIVATE
${CMAKE_SOURCE_DIR}/src)
add_subdirectory(system)

3
src/runtime/rtinternal.h Executable file
View File

@ -0,0 +1,3 @@
#include "base.h"
extern int main(int argc, BYTE** argv, BYTE** envp);

9
src/runtime/runtime.c Executable file
View File

@ -0,0 +1,9 @@
#include "rtinternal.h"
int rt_bootstrap(void)
{
// Bootstraping => based.
// Invoke main.
// Return exit code to caller.
return 420;
}

View File

@ -0,0 +1,8 @@
# runtime => system
if(UNIX)
add_subdirectory(unix)
endif()
if(WIN32)
add_subdirectory(win32)
endif()

View File

@ -0,0 +1,3 @@
# runtime => system => unix
target_sources(runtime PRIVATE "unixrt.c" "crt.S")

17
src/runtime/system/unix/crt.S Executable file
View File

@ -0,0 +1,17 @@
//#include "../syscalls.h"
.globl _start
.globl rt_unix_start
.text
// Start point unix.
_start:
xor %rbp, %rbp // Clear stack frame pointer
mov (%rsp), %rdi // Movq argc to rdi
lea 0x8(%rsp), %rsi // Get the argv address
lea 16(%rsp,%rdi,8), %rdx // Get the envp address
call rt_unix_start // Start c function
hlt // Halt if the lib or app does not call the exit function.

View File

@ -0,0 +1,6 @@
#include "base.h"
void rt_unix_start(int argc, BYTE** argv, BYTE** envp)
{
//TODO: Callback => main & syscall to exit group
}

View File

@ -0,0 +1,3 @@
# runtime => system => win32
target_sources(runtime PRIVATE "winrt.c")

View File

@ -0,0 +1,59 @@
#include "base.h"
#include "windows.h"
// Windows(Win32) of course needs to be different...
// Use the wide entry
#pragma comment(linker,"/ENTRY:wmainCRTStartup")
// Main
//extern int WINAPI mainCRTStartup();
int WINAPI mainCRTStartup() // => main
{
return 0;
}
//extern int __CRTDECL wmain(int argc, wchar_t** argv, wchar_t** envp);
int WINAPI wmainCRTStartup() // => wmain
{
return 0;
}
// WinMain
//extern int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
int WINAPI WinMainCRTStartup() // => WinMain
{
return 0;
}
//extern int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd);
int wWinMainCRTStartup() // => wWinMain
{
return 0;
}
// RTC stuff
BOOL WINAPI _RTC_InitBase()
{
return TRUE;
}
BOOL WINAPI _RTC_Shutdown()
{
return TRUE;
}
// Library
/* #ifdef MRTDLL
extern BOOL __clrcall DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved);
#else
extern BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved);
#endif // MRTDLL */
BOOL WINAPI _DllMainCRTStartup(HINSTANCE const instance, DWORD const reason, LPVOID const reserved) // => DllMain
{
(void)instance;
(void)reason;
(void)reserved;
//TODO: Handle dllmain
return TRUE;
}