1 cmake_minimum_required(VERSION 3.0)
2 project(lightrec LANGUAGES C VERSION 0.5)
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
39 list(APPEND LIGHTREC_HEADERS
53 option(ENABLE_FIRST_PASS "Run the interpreter as first-pass optimization" ON)
55 option(ENABLE_THREADED_COMPILER "Enable threaded compiler" ON)
56 if (ENABLE_THREADED_COMPILER)
57 list(APPEND LIGHTREC_SOURCES recompiler.c reaper.c)
59 if (NOT ENABLE_FIRST_PASS)
60 message(SEND_ERROR "Threaded compiler requires first-pass optimization")
61 endif (NOT ENABLE_FIRST_PASS)
62 endif (ENABLE_THREADED_COMPILER)
64 option(OPT_REMOVE_DIV_BY_ZERO_SEQ "(optimization) Remove div-by-zero check sequence" ON)
65 option(OPT_REPLACE_MEMSET "(optimization) Detect and replace memset with host variant" ON)
66 option(OPT_DETECT_IMPOSSIBLE_BRANCHES "(optimization) Detect impossible branches" ON)
67 option(OPT_TRANSFORM_OPS "(optimization) Transform opcodes" ON)
68 option(OPT_LOCAL_BRANCHES "(optimization) Detect local branches" ON)
69 option(OPT_SWITCH_DELAY_SLOTS "(optimization) Switch delay slots" ON)
70 option(OPT_FLAG_STORES "(optimization) Flag stores that don't require invalidation" ON)
71 option(OPT_FLAG_IO "(optimization) Flag I/O opcodes whose target is known" ON)
72 option(OPT_FLAG_MULT_DIV "(optimization) Flag MULT/DIV that only use one of HI/LO" ON)
73 option(OPT_EARLY_UNLOAD "(optimization) Unload registers early" ON)
75 include_directories(${CMAKE_CURRENT_BINARY_DIR})
77 add_library(${PROJECT_NAME} ${LIGHTREC_SOURCES} ${LIGHTREC_HEADERS})
78 set_target_properties(${PROJECT_NAME} PROPERTIES
79 VERSION ${PROJECT_VERSION}
80 SOVERSION ${PROJECT_VERSION_MAJOR}
81 PUBLIC_HEADER lightrec.h
83 C_STANDARD_REQUIRED ON
87 if (CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
88 target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wno-parentheses)
90 if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
91 target_compile_options(${PROJECT_NAME} PRIVATE -Wno-initializer-overrides)
94 option(ENABLE_TINYMM "Enable optional libtinymm dependency" OFF)
96 find_library(TINYMM_LIBRARIES tinymm REQUIRED)
97 find_path(TINYMM_INCLUDE_DIR tinymm.h REQUIRED)
99 include_directories(${TINYMM_INCLUDE_DIR})
100 target_link_libraries(${PROJECT_NAME} PRIVATE ${TINYMM_LIBRARIES})
101 endif (ENABLE_TINYMM)
103 if (ENABLE_THREADED_COMPILER)
104 find_library(PTHREAD_LIBRARIES pthread REQUIRED)
105 find_path(PTHREAD_INCLUDE_DIR pthread.h REQUIRED)
107 include_directories(${PTHREAD_INCLUDE_DIR})
108 target_link_libraries(${PROJECT_NAME} PRIVATE ${PTHREAD_LIBRARIES})
109 endif (ENABLE_THREADED_COMPILER)
111 option(ENABLE_CODE_BUFFER "Enable external code buffer" OFF)
112 if (ENABLE_CODE_BUFFER)
113 target_sources(${PROJECT_NAME} PRIVATE tlsf/tlsf.c)
114 target_include_directories(${PROJECT_NAME} PRIVATE tlsf)
115 endif (ENABLE_CODE_BUFFER)
117 if (ENABLE_CODE_BUFFER AND ENABLE_THREADED_COMPILER)
118 message(SEND_ERROR "External code buffer cannot be used along with the threaded compiler")
119 endif (ENABLE_CODE_BUFFER AND ENABLE_THREADED_COMPILER)
121 find_library(LIBLIGHTNING lightning REQUIRED)
122 find_path(LIBLIGHTNING_INCLUDE_DIR lightning.h REQUIRED)
124 include_directories(${LIBLIGHTNING_INCLUDE_DIR})
125 target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBLIGHTNING})
127 if (LOG_LEVEL STREQUAL Debug)
128 set(ENABLE_DISASSEMBLER ON)
129 target_sources(${PROJECT_NAME} PRIVATE disassembler.c)
132 configure_file(lightrec-config.h.cmakein lightrec-config.h @ONLY)
134 include(GNUInstallDirs)
135 install(TARGETS ${PROJECT_NAME}
136 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
137 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
138 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
139 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}