2024-02-04 15:23:32 +00:00
|
|
|
cmake_minimum_required(VERSION 3.15...3.25)
|
|
|
|
|
|
|
|
# Canonical project structure
|
|
|
|
# Header must be included this way: #include <module/header.h>
|
|
|
|
#
|
|
|
|
# ├─ res
|
2024-05-17 13:39:07 +00:00
|
|
|
# ├─ dep
|
|
|
|
# │ └─ klib
|
2024-02-04 15:23:32 +00:00
|
|
|
# ├─ 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)
|
|
|
|
|
2024-05-10 13:09:52 +00:00
|
|
|
set(CMAKE_C_STANDARD 23)
|
|
|
|
set(CMAKE_C_STANDARD_REQUIRED TRUE)
|
|
|
|
|
2024-05-02 06:26:18 +00:00
|
|
|
set(GEMSTONE_TEST_DIR ${PROJECT_SOURCE_DIR}/tests)
|
|
|
|
set(GEMSTONE_BINARY_DIR ${PROJECT_SOURCE_DIR}/bin)
|
|
|
|
|
|
|
|
include(CTest)
|
|
|
|
|
|
|
|
if(BUILD_TESTING)
|
|
|
|
add_subdirectory(tests)
|
|
|
|
endif()
|
|
|
|
|
2024-02-13 21:28:24 +00:00
|
|
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
# ------------------------------------------------ #
|
|
|
|
# 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)
|
|
|
|
|
2024-04-24 20:47:59 +00:00
|
|
|
# remove dependency when compiling with MSVC on windows
|
|
|
|
if (MSVC)
|
|
|
|
add_compile_definitions(YY_NO_UNISTD_H)
|
|
|
|
endif()
|
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
# ------------------------------------------------ #
|
|
|
|
# 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}
|
2024-05-15 17:19:03 +00:00
|
|
|
|
2024-05-13 09:33:23 +00:00
|
|
|
COMMAND bison
|
2024-05-14 10:37:52 +00:00
|
|
|
ARGS -Wno-yacc -Wcounterexamples -d -o ${YACC_GENERATED_SOURCE_FILE} ${YACC_SOURCE_FILE}
|
2024-05-15 17:19:03 +00:00
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
COMMENT "generate C source file for parser"
|
|
|
|
VERBATIM)
|
|
|
|
|
2024-05-17 13:39:07 +00:00
|
|
|
# ------------------------------------------------ #
|
2024-05-18 10:25:13 +00:00
|
|
|
# Setup Glib 2.0 #
|
2024-05-17 13:39:07 +00:00
|
|
|
# ------------------------------------------------ #
|
|
|
|
|
2024-05-18 10:25:13 +00:00
|
|
|
find_package(PkgConfig REQUIRED)
|
|
|
|
pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0)
|
2024-05-17 13:39:07 +00:00
|
|
|
|
2024-05-16 10:29:11 +00:00
|
|
|
# ------------------------------------------------ #
|
|
|
|
# LLVM backend #
|
|
|
|
# ------------------------------------------------ #
|
|
|
|
|
2024-05-27 17:53:22 +00:00
|
|
|
find_package(PkgConfig REQUIRED)
|
|
|
|
pkg_search_module(LLVM REQUIRED IMPORTED_TARGET llvm17)
|
2024-05-16 10:29:11 +00:00
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
# ------------------------------------------------ #
|
|
|
|
# Source #
|
|
|
|
# ------------------------------------------------ #
|
|
|
|
|
2024-05-17 13:39:07 +00:00
|
|
|
include_directories(${PROJECT_SOURCE_DIR}/src)
|
2024-05-18 10:25:13 +00:00
|
|
|
include_directories(PRIVATE ${GLIB_INCLUDE_DIRS})
|
2024-05-27 17:53:22 +00:00
|
|
|
include_directories(PRIVATE ${LLVM_INCLUDE_DIRS})
|
2024-05-17 13:39:07 +00:00
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
file(GLOB_RECURSE SOURCE_FILES src/*.c)
|
|
|
|
|
|
|
|
# define default compile flags
|
2024-04-24 20:37:21 +00:00
|
|
|
if (MSVC)
|
|
|
|
set(FLAGS /Wall /W3 /permissive)
|
|
|
|
else()
|
2024-05-17 13:39:07 +00:00
|
|
|
set(FLAGS -Wall -Wextra -Wpedantic)
|
2024-04-24 20:37:21 +00:00
|
|
|
endif()
|
2024-02-04 15:23:32 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------ #
|
|
|
|
# Target RELEASE #
|
|
|
|
# ------------------------------------------------ #
|
|
|
|
|
|
|
|
add_executable(release
|
|
|
|
${SOURCE_FILES}
|
|
|
|
${LEX_GENERATED_SOURCE_FILE}
|
|
|
|
${YACC_GENERATED_SOURCE_FILE})
|
|
|
|
|
|
|
|
set_target_properties(release
|
|
|
|
PROPERTIES
|
|
|
|
OUTPUT_NAME "gsc"
|
2024-05-02 06:26:18 +00:00
|
|
|
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/release)
|
2024-02-04 15:23:32 +00:00
|
|
|
|
2024-05-18 10:25:13 +00:00
|
|
|
target_link_libraries(release PkgConfig::GLIB)
|
2024-05-17 13:39:07 +00:00
|
|
|
|
2024-04-24 20:37:21 +00:00
|
|
|
# FIXME: cannot compile with /O2 because of /RTC1 flag
|
|
|
|
if (MSVC)
|
|
|
|
set(RELEASE_FLAGS)
|
|
|
|
else()
|
|
|
|
set(RELEASE_FLAGS -m64 -O3 -fprefetch-loop-arrays -mrecip)
|
|
|
|
endif()
|
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
# 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
|
2024-04-24 20:37:21 +00:00
|
|
|
target_compile_options(release PUBLIC ${FLAGS} ${RELEASE_FLAGS})
|
2024-02-04 15:23:32 +00:00
|
|
|
|
|
|
|
# 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"
|
2024-05-02 06:26:18 +00:00
|
|
|
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/debug)
|
2024-02-04 15:23:32 +00:00
|
|
|
|
2024-05-18 10:25:13 +00:00
|
|
|
target_link_libraries(debug PkgConfig::GLIB)
|
2024-05-17 13:39:07 +00:00
|
|
|
|
2024-04-24 20:37:21 +00:00
|
|
|
if (MSVC)
|
|
|
|
set(DEBUG_FLAGS /DEBUG)
|
|
|
|
else()
|
|
|
|
set(DEBUG_FLAGS -g)
|
|
|
|
endif()
|
|
|
|
|
2024-02-04 15:23:32 +00:00
|
|
|
# compiler flags targeting a GCC debug environment
|
2024-04-24 20:37:21 +00:00
|
|
|
target_compile_options(debug PUBLIC ${FLAGS} ${DEBUG_FLAGS})
|
2024-02-04 15:23:32 +00:00
|
|
|
|
|
|
|
# add src directory as include path
|
|
|
|
target_include_directories(debug PUBLIC src)
|
2024-04-12 21:06:10 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------ #
|
|
|
|
# Target Code Check #
|
|
|
|
# ------------------------------------------------ #
|
|
|
|
|
|
|
|
# Same as debug but will fail on warnings
|
|
|
|
# use as check
|
|
|
|
|
|
|
|
add_executable(check
|
|
|
|
${SOURCE_FILES}
|
|
|
|
${LEX_GENERATED_SOURCE_FILE}
|
|
|
|
${YACC_GENERATED_SOURCE_FILE})
|
|
|
|
|
|
|
|
set_target_properties(check
|
|
|
|
PROPERTIES
|
|
|
|
OUTPUT_NAME "gsc"
|
2024-05-02 06:26:18 +00:00
|
|
|
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/check)
|
2024-04-12 21:06:10 +00:00
|
|
|
|
2024-05-18 10:25:13 +00:00
|
|
|
target_link_libraries(check PkgConfig::GLIB)
|
2024-05-17 13:39:07 +00:00
|
|
|
|
2024-04-24 20:37:21 +00:00
|
|
|
if (MSVC)
|
|
|
|
set(CHECK_FLAGS /DEBUG /WX)
|
|
|
|
else()
|
2024-04-24 20:47:59 +00:00
|
|
|
set(DEBUG_FLAGS -g -Werror)
|
2024-04-24 20:37:21 +00:00
|
|
|
endif()
|
|
|
|
|
2024-04-12 21:06:10 +00:00
|
|
|
# compiler flags targeting a GCC debug environment
|
|
|
|
# extra -Werror flag to treat warnings as error to make github action fail on warning
|
2024-04-24 20:37:21 +00:00
|
|
|
target_compile_options(check PUBLIC ${FLAGS} ${DEBUG_FLAGS})
|
2024-04-12 21:06:10 +00:00
|
|
|
|
|
|
|
# add src directory as include path
|
|
|
|
target_include_directories(check PUBLIC src)
|
|
|
|
|