added canonical project structure

This commit is contained in:
Sven Vogel 2024-02-04 16:23:32 +01:00
commit 9c02a86a51
8 changed files with 210 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
bin
cmake-build-debug
.vscode
.idea
tmp
.git

102
CMakeLists.txt Normal file
View File

@ -0,0 +1,102 @@
cmake_minimum_required(VERSION 3.15...3.25)
# Canonical project structure
# Header must be included this way: #include <module/header.h>
#
# res
# src
# lex
# lexer.l
# yacc
# parser.y
# module
# header.h
# source.c
# main.c
# tests
# test.c
project(gemstone
VERSION 0.1.0
DESCRIPTION "programming language compiler"
LANGUAGES C)
# ------------------------------------------------ #
# Lex #
# ------------------------------------------------ #
set(LEX_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.l)
set(LEX_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c)
add_custom_command(OUTPUT ${LEX_GENERATED_SOURCE_FILE}
COMMAND lex
ARGS -o ${LEX_GENERATED_SOURCE_FILE} ${LEX_SOURCE_FILE}
COMMENT "generate C source file for lexer"
VERBATIM)
# ------------------------------------------------ #
# Yacc #
# ------------------------------------------------ #
set(YACC_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.y)
set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c)
add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE}
COMMAND yacc
ARGS -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE}
COMMENT "generate C source file for parser"
VERBATIM)
# ------------------------------------------------ #
# Source #
# ------------------------------------------------ #
file(GLOB_RECURSE SOURCE_FILES src/*.c)
# define default compile flags
set(FLAGS -Wall -Wconversion)
# ------------------------------------------------ #
# Target RELEASE #
# ------------------------------------------------ #
add_executable(release
${SOURCE_FILES}
${LEX_GENERATED_SOURCE_FILE}
${YACC_GENERATED_SOURCE_FILE})
set_target_properties(release
PROPERTIES
OUTPUT_NAME "gsc"
RUNTIME_OUTPUT_DIRECTORY "bin/release")
# compiler flags targeting a 64-bit GCC release environment
# flags:
# - m64: build for 64-bit
# - O3: optimization level 3
# - fprefetch-loop-arrays: pre load arrays used in loops by using prefetch instruction
# - mrecip: make use RCPSS and RSQRTSS instructions
target_compile_options(release PUBLIC ${FLAGS} -m64 -O3 -fprefetch-loop-arrays -mrecip)
# add src directory as include path
target_include_directories(release PUBLIC src)
# ------------------------------------------------ #
# Target DEBUG #
# ------------------------------------------------ #
add_executable(debug
${SOURCE_FILES}
${LEX_GENERATED_SOURCE_FILE}
${YACC_GENERATED_SOURCE_FILE})
set_target_properties(debug
PROPERTIES
OUTPUT_NAME "gsc"
RUNTIME_OUTPUT_DIRECTORY "bin/debug")
# compiler flags targeting a GCC debug environment
target_compile_options(debug PUBLIC ${FLAGS} -g)
# add src directory as include path
target_include_directories(debug PUBLIC src)

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM servostar/gemstone:sdk-0.1.0-alma-9.3
LABEL authors="servostar"
LABEL version="0.1.0"
LABEL description="docker image for setting up the build pipeline on SDK"
LABEL website="https://github.com/Servostar/gemstone"
COPY --chown=lorang src /home/lorang/src
COPY --chown=lorang CMakeLists.txt /home/lorang/
RUN cmake .

47
README.md Normal file
View File

@ -0,0 +1,47 @@
# Gemstone
Gemstone is a programming language compiler written in C with lex and yacc.
## Build
The build pipeline is configured with CMake in the file CMakeLists.txt.
In order to avoid dependency and configuration issues the recommended way to build is by using the provided docker containers.
All tools required for building (`cmake`, `make`, `gcc`, `lex`, `yacc`) are installed inside the SDK container (see Dockerfile sdk/Dockerfile).
For creating the build pipeline build the Dockerfile in the root folder of this repository. This takes the current SDK and copies the source files into the home of the build user.
Then the make targets are generated. Running `make release` will build gemstone from source in release mode.
The generated binaries can be found either in `bin/release/gsc` or `bin/debug/gsc` depending on the chosen target.
The following graph visualizes the build pipeline:
```
SDK (environment)
│ configure build environment
│ cmake, make, gcc, yacc, lex
Devkit (pipeline)
│ create build pipeline
│ create make targets
Pipeline
yacc (generate files) GCC (compile) Extra Source Files (src/*.c)
│ │ │
├─ parser.tab.h ─────────────►│◄────────────────────┘
│ │
└─ parser.tab.c ─────────────►│
lex (generate file) │
│ │
└─ lexer.ll.c ──────────────►│
gsc
```
## Docker images
Currently, the SDK is based on Almalinux 9.3, an open source distro binary compatible to RHEL 9.3.
The following images can be found in the offical repository at [Docker Bub](https://hub.docker.com/r/servostar/gemstone):
- SDK
- Devkit

13
sdk/Dockerfile Normal file
View File

@ -0,0 +1,13 @@
FROM almalinux:9.3
LABEL authors="servostar"
LABEL version="0.1.0"
LABEL description="base image for building the gemstone programming language compiler"
LABEL website="https://github.com/Servostar/gemstone"
# install dependencies
RUN yum install cmake gcc flex byacc -y
# create user for build
RUN adduser lorang
WORKDIR /home/lorang
USER lorang

11
src/lex/lexer.l Normal file
View File

@ -0,0 +1,11 @@
%option noyywrap
%{
#include <yacc/parser.tab.h>
int yyLineNumber = 1;
int yylex();
%}
%%
.;
%%

6
src/main.c Normal file
View File

@ -0,0 +1,6 @@
#include <yacc/parser.tab.h>
int main() {
yyparse();
return 0;
}

15
src/yacc/parser.y Normal file
View File

@ -0,0 +1,15 @@
%{
extern int yylineno;
int yyerror(char*);
extern int yylex();
%}
%%
program: ;
%%
int yyerror(char *s) {
return 0;
}