1 cmake_minimum_required(VERSION 3.0)
2 project(lightrec LANGUAGES C VERSION 0.7)
4 set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries")
5 if (NOT BUILD_SHARED_LIBS)
6 add_definitions(-DLIGHTREC_STATIC)
7 endif (NOT BUILD_SHARED_LIBS)
10 set(LOG_LEVEL Info CACHE STRING "Log level" FORCE)
11 set_property(CACHE LOG_LEVEL PROPERTY STRINGS NoLog Error Warning Info Debug)
14 if (NOT CMAKE_BUILD_TYPE)
15 set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
16 "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel."
18 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS None Debug Release RelWithDebInfo MinSizeRel)
21 string(TOUPPER ${LOG_LEVEL} LIGHTREC_LOG_LEVEL)
22 add_definitions(-DLOG_LEVEL=${LIGHTREC_LOG_LEVEL}_L)
24 if (CMAKE_COMPILER_IS_GNUCC)
25 add_compile_options(-fvisibility=hidden)
28 set(HAS_DEFAULT_ELM ${CMAKE_COMPILER_IS_GNUCC})
30 list(APPEND LIGHTREC_SOURCES
40 list(APPEND LIGHTREC_HEADERS
55 option(ENABLE_FIRST_PASS "Run the interpreter as first-pass optimization" ON)
57 option(ENABLE_THREADED_COMPILER "Enable threaded compiler" OFF)
58 if (ENABLE_THREADED_COMPILER)
59 list(APPEND LIGHTREC_SOURCES recompiler.c reaper.c)
61 if (NOT ENABLE_FIRST_PASS)
62 message(SEND_ERROR "Threaded compiler requires first-pass optimization")
63 endif (NOT ENABLE_FIRST_PASS)
64 endif (ENABLE_THREADED_COMPILER)
66 option(OPT_REMOVE_DIV_BY_ZERO_SEQ "(optimization) Remove div-by-zero check sequence" ON)
67 option(OPT_REPLACE_MEMSET "(optimization) Detect and replace memset with host variant" ON)
68 option(OPT_DETECT_IMPOSSIBLE_BRANCHES "(optimization) Detect impossible branches" ON)
69 option(OPT_TRANSFORM_OPS "(optimization) Transform opcodes" ON)
70 option(OPT_LOCAL_BRANCHES "(optimization) Detect local branches" ON)
71 option(OPT_SWITCH_DELAY_SLOTS "(optimization) Switch delay slots" ON)
72 option(OPT_FLAG_STORES "(optimization) Flag stores that don't require invalidation" ON)
73 option(OPT_FLAG_IO "(optimization) Flag I/O opcodes whose target is known" ON)
74 option(OPT_FLAG_MULT_DIV "(optimization) Flag MULT/DIV that only use one of HI/LO" ON)
75 option(OPT_EARLY_UNLOAD "(optimization) Unload registers early" ON)
77 include_directories(${CMAKE_CURRENT_BINARY_DIR})
79 add_library(${PROJECT_NAME} ${LIGHTREC_SOURCES} ${LIGHTREC_HEADERS})
80 set_target_properties(${PROJECT_NAME} PROPERTIES
81 VERSION ${PROJECT_VERSION}
82 SOVERSION ${PROJECT_VERSION_MAJOR}
83 PUBLIC_HEADER lightrec.h
85 C_STANDARD_REQUIRED ON
89 if (CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
90 target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-parentheses)
92 if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
93 target_compile_options(${PROJECT_NAME} PRIVATE -Wno-initializer-overrides)
96 if (ENABLE_THREADED_COMPILER)
97 find_library(PTHREAD_LIBRARIES pthread REQUIRED)
98 find_path(PTHREAD_INCLUDE_DIR pthread.h REQUIRED)
100 include_directories(${PTHREAD_INCLUDE_DIR})
101 target_link_libraries(${PROJECT_NAME} PRIVATE ${PTHREAD_LIBRARIES})
102 endif (ENABLE_THREADED_COMPILER)
104 option(ENABLE_CODE_BUFFER "Enable external code buffer" ON)
105 if (ENABLE_CODE_BUFFER)
106 target_sources(${PROJECT_NAME} PRIVATE tlsf/tlsf.c)
107 target_include_directories(${PROJECT_NAME} PRIVATE tlsf)
108 endif (ENABLE_CODE_BUFFER)
110 find_library(LIBLIGHTNING lightning REQUIRED)
111 find_path(LIBLIGHTNING_INCLUDE_DIR lightning.h REQUIRED)
113 include_directories(${LIBLIGHTNING_INCLUDE_DIR})
114 target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBLIGHTNING})
116 if (LOG_LEVEL STREQUAL Debug)
117 set(ENABLE_DISASSEMBLER ON)
118 target_sources(${PROJECT_NAME} PRIVATE disassembler.c)
121 configure_file(lightrec-config.h.cmakein lightrec-config.h @ONLY)
123 include(GNUInstallDirs)
124 install(TARGETS ${PROJECT_NAME}
125 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
126 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
127 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
128 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}