Skip to content

Commit 1052a1b

Browse files
author
Gin
committed
separate build target into a library and binary
1 parent da2ee4a commit 1052a1b

File tree

1 file changed

+74
-52
lines changed

1 file changed

+74
-52
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ set(CMAKE_AUTOUIC ON)
3333
add_custom_target(build-time-make-directory ALL
3434
COMMAND ${CMAKE_COMMAND} -E make_directory Assembly/)
3535

36-
# Find repo root folder path
37-
string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "Arduino-Source-Internal" internal_repo_position)
38-
3936
# Determine if this is an internal repo and normalize REPO_ROOT_DIR
40-
if(internal_repo_position EQUAL -1)
41-
# We are building the public repo
37+
# CMAKE_CURRENT_SOURCE_DIR: the full path to this CmakeLists.txt
38+
# Find if "Arduino-Source-Internal" is in the path and store the find result to internal_repo_position
39+
string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "Arduino-Source-Internal" internal_repo_position)
40+
if(internal_repo_position EQUAL -1) # no "Arduino-Source-Internal" in the path
41+
# We are building the public repo, CMakeLists.txt is one level deep in the root repo dir
4242
set(REPO_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
4343
else()
44-
# We are building the internal repo
44+
# We are building the internal repo, CMakeLists.txt is three levels deep in the root repo dir
4545
set(REPO_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../")
4646
endif()
4747

@@ -154,7 +154,18 @@ link_directories(${CMAKE_CURRENT_LIST_DIR})
154154

155155
# add the list of all code file paths as ${MAIN_SOURCES}
156156
include(SourceFiles.cmake)
157-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}../../ FILES ${MAIN_SOURCES})
157+
158+
# Separate sources into library and executable components
159+
set(LIBRARY_SOURCES ${MAIN_SOURCES})
160+
list(REMOVE_ITEM LIBRARY_SOURCES "Source/CommonFramework/Main.cpp")
161+
set(EXECUTABLE_SOURCES "Source/CommonFramework/Main.cpp")
162+
163+
# Organize source files in IDE project view
164+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../../ FILES ${LIBRARY_SOURCES})
165+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../../ FILES ${EXECUTABLE_SOURCES})
166+
167+
# Create library target first
168+
add_library(SerialProgramsLib STATIC ${LIBRARY_SOURCES})
158169

159170
if (APPLE)
160171
set(SerialPrograms_ICON ${CMAKE_CURRENT_SOURCE_DIR}/../IconResource/icon.icns)
@@ -163,7 +174,7 @@ if (APPLE)
163174
# Links on how to create a MacOS app bundle with cmake
164175
# https://stackoverflow.com/questions/53560288/how-to-create-a-macos-app-bundle-with-cmake
165176
# https://cmake.org/cmake/help/latest/command/add_executable.html
166-
add_executable(SerialPrograms MACOSX_BUNDLE ${SerialPrograms_ICON} ${MAIN_SOURCES})
177+
add_executable(SerialPrograms MACOSX_BUNDLE ${SerialPrograms_ICON} ${EXECUTABLE_SOURCES})
167178
set_target_properties(SerialPrograms PROPERTIES
168179
BUNDLE True
169180
MACOSX_BUNDLE_GUI_IDENTIFIER PokemonAutomation.SerialPrograms
@@ -205,39 +216,50 @@ elseif (WIN32 AND DEPLOY_FILES)
205216
DEPENDS ${DISCORD_LIB_DEBUG} ${DISCORD_LIB_RELEASE} ${OPENCV_DEBUG}
206217
)
207218

208-
add_executable(SerialPrograms WIN32 ${MAIN_SOURCES})
219+
add_executable(SerialPrograms WIN32 ${EXECUTABLE_SOURCES})
209220
add_dependencies(SerialPrograms ExtractDependencies)
210221
add_dependencies(SerialPrograms PackagesRepo)
211222
else()
212-
add_executable(SerialPrograms WIN32 ${MAIN_SOURCES})
223+
add_executable(SerialPrograms WIN32 ${EXECUTABLE_SOURCES})
213224
endif()
214225

215-
set_target_properties(SerialPrograms PROPERTIES LINKER_LANGUAGE CXX)
216-
target_link_libraries(SerialPrograms PRIVATE Qt${QT_MAJOR}::Widgets Qt${QT_MAJOR}::SerialPort Qt${QT_MAJOR}::Multimedia Qt${QT_MAJOR}::MultimediaWidgets)
217-
target_link_libraries(SerialPrograms PRIVATE Threads::Threads)
226+
target_link_libraries(SerialPrograms PRIVATE SerialProgramsLib)
227+
228+
# Function to apply common properties to both library and executable targets
229+
function(apply_common_target_properties target_name)
230+
set_target_properties(${target_name} PROPERTIES LINKER_LANGUAGE CXX)
231+
target_link_libraries(${target_name} PRIVATE Qt${QT_MAJOR}::Widgets Qt${QT_MAJOR}::SerialPort Qt${QT_MAJOR}::Multimedia Qt${QT_MAJOR}::MultimediaWidgets)
232+
target_link_libraries(${target_name} PRIVATE Threads::Threads)
233+
234+
#add defines
235+
target_compile_definitions(${target_name} PRIVATE NOMINMAX DPP_NO_DEPRECATED)
218236

219-
#add defines
220-
target_compile_definitions(SerialPrograms PRIVATE NOMINMAX DPP_NO_DEPRECATED)
237+
#add include directory
238+
target_include_directories(${target_name} SYSTEM PRIVATE ../3rdParty/)
239+
target_include_directories(${target_name} PRIVATE ../ ../../Internal/ Source/)
240+
target_link_directories(${target_name} PRIVATE ../3rdPartyBinaries/)
241+
endfunction()
242+
243+
# Apply common properties to both targets
244+
apply_common_target_properties(SerialProgramsLib)
245+
apply_common_target_properties(SerialPrograms)
221246

222247
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../Internal/SerialPrograms/Internal0.cpp")
248+
target_compile_definitions(SerialProgramsLib PRIVATE PA_OFFICIAL)
223249
target_compile_definitions(SerialPrograms PRIVATE PA_OFFICIAL)
224-
target_sources(SerialPrograms PRIVATE ../../Internal/SerialPrograms/NintendoSwitch_TestPrograms.cpp)
225-
target_sources(SerialPrograms PRIVATE ../../Internal/SerialPrograms/NintendoSwitch_TestPrograms.h)
226-
target_sources(SerialPrograms PRIVATE ../../Internal/SerialPrograms/Internal0.cpp)
227-
target_sources(SerialPrograms PRIVATE ../../Internal/SerialPrograms/Internal1.cpp)
250+
target_sources(SerialProgramsLib PRIVATE ../../Internal/SerialPrograms/NintendoSwitch_TestPrograms.cpp)
251+
target_sources(SerialProgramsLib PRIVATE ../../Internal/SerialPrograms/NintendoSwitch_TestPrograms.h)
252+
target_sources(SerialProgramsLib PRIVATE ../../Internal/SerialPrograms/Internal0.cpp)
253+
target_sources(SerialProgramsLib PRIVATE ../../Internal/SerialPrograms/Internal1.cpp)
228254
endif()
229255

230-
#add include directory
231-
target_include_directories(SerialPrograms SYSTEM PRIVATE ../3rdParty/)
232-
target_include_directories(SerialPrograms PRIVATE ../ ../../Internal/ Source/)
233-
target_link_directories(SerialPrograms PRIVATE ../3rdPartyBinaries/)
234256

235257

236258

237259

238260
if (WIN32)
239261
add_library(OpenCV_lib IMPORTED UNKNOWN)
240-
target_include_directories(SerialPrograms SYSTEM PRIVATE ../3rdParty/opencv-4.11.0/)
262+
target_include_directories(SerialProgramsLib SYSTEM PRIVATE ../3rdParty/opencv-4.11.0/)
241263
set_target_properties(OpenCV_lib PROPERTIES
242264
IMPORTED_LOCATION_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/../3rdPartyBinaries/opencv_world4110.lib
243265
IMPORTED_LOCATION_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/../3rdPartyBinaries/opencv_world4110d.lib
@@ -248,7 +270,7 @@ if (WIN32)
248270
)
249271

250272
add_library(ONNX_lib IMPORTED UNKNOWN)
251-
target_include_directories(SerialPrograms SYSTEM PRIVATE ../3rdParty/ONNX/)
273+
target_include_directories(SerialProgramsLib SYSTEM PRIVATE ../3rdParty/ONNX/)
252274
set_target_properties(ONNX_lib PROPERTIES
253275
IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../3rdPartyBinaries/onnxruntime.lib
254276
)
@@ -269,7 +291,7 @@ if (WIN32)
269291
)
270292

271293
add_library(discord_lib IMPORTED UNKNOWN)
272-
target_include_directories(SerialPrograms SYSTEM PRIVATE ../3rdParty/discord_social_sdk_win/)
294+
target_include_directories(SerialProgramsLib SYSTEM PRIVATE ../3rdParty/discord_social_sdk_win/)
273295
set_target_properties(discord_lib PROPERTIES
274296
IMPORTED_LOCATION_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/../3rdPartyBinaries/discord_social_sdk_win/lib/release/discord_partner_sdk.lib
275297
IMPORTED_LOCATION_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/../3rdPartyBinaries/discord_social_sdk_win/lib/debug/discord_partner_sdk.lib
@@ -280,7 +302,7 @@ if (WIN32)
280302
)
281303

282304
target_link_libraries(
283-
SerialPrograms PRIVATE
305+
SerialProgramsLib PRIVATE
284306
tesseractPA.lib
285307
OpenCV_lib
286308
ONNX_lib
@@ -290,17 +312,17 @@ if (WIN32)
290312
)
291313

292314
target_compile_definitions(
293-
SerialPrograms PRIVATE
315+
SerialProgramsLib PRIVATE
294316
_CRT_SECURE_NO_WARNINGS
295317
_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR
296318
PA_TESSERACT
297319
PA_DPP
298320
PA_SOCIAL_SDK
299321
)
300322

301-
target_compile_options(SerialPrograms PRIVATE /FAs /FaAssembly/ /MP /W4 /WX /external:anglebrackets /external:W0 /utf-8)
302-
target_compile_options(SerialPrograms PRIVATE /wd5054) # Deprecated enum arithemtic
303-
target_compile_options(SerialPrograms PRIVATE /wd4505) # unreferenced local function has been removed
323+
target_compile_options(SerialProgramsLib PRIVATE /FAs /FaAssembly/ /MP /W4 /WX /external:anglebrackets /external:W0 /utf-8)
324+
target_compile_options(SerialProgramsLib PRIVATE /wd5054) # Deprecated enum arithemtic
325+
target_compile_options(SerialProgramsLib PRIVATE /wd4505) # unreferenced local function has been removed
304326

305327
set(ARCH_FLAGS_09_Nehalem /W4) # Dummy parameter
306328
set(ARCH_FLAGS_13_Haswell /arch:AVX2)
@@ -342,14 +364,14 @@ else() # macOS and Linux
342364
if(EXISTS "${ONNXRUNTIME_HEADER_DIR}")
343365
# we have ONNX Runtime built from source
344366
message("Use ONNX Runtime built from source at ${ONNXRUNTIME_ROOTDIR}")
345-
target_include_directories(SerialPrograms PRIVATE ${ONNXRUNTIME_HEADER_DIR})
346-
link_directories(SerialPrograms PRIVATE ${ONNXRUNTIME_ROOTDIR}/build/MacOS/Release)
347-
target_link_libraries(SerialPrograms PRIVATE ${ONNXRUNTIME_ROOTDIR}/build/MacOS/Release/libonnxruntime.dylib)
367+
target_include_directories(SerialProgramsLib PRIVATE ${ONNXRUNTIME_HEADER_DIR})
368+
link_directories(SerialProgramsLib PRIVATE ${ONNXRUNTIME_ROOTDIR}/build/MacOS/Release)
369+
target_link_libraries(SerialProgramsLib PRIVATE ${ONNXRUNTIME_ROOTDIR}/build/MacOS/Release/libonnxruntime.dylib)
348370
else()
349371
message("Built-from-source ONNX Runtime folder ${ONNXRUNTIME_ROOTDIR} does not exist.")
350372
message("Use ONNX Runtime installed by Homebrew.")
351-
target_include_directories(SerialPrograms PRIVATE ${HOMEBREW_PREFIX}/include/onnxruntime)
352-
target_link_libraries(SerialPrograms PRIVATE ${HOMEBREW_PREFIX}/lib/libonnxruntime.dylib)
373+
target_include_directories(SerialProgramsLib PRIVATE ${HOMEBREW_PREFIX}/include/onnxruntime)
374+
target_link_libraries(SerialProgramsLib PRIVATE ${HOMEBREW_PREFIX}/lib/libonnxruntime.dylib)
353375
endif()
354376
# Find OpenCV
355377
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm64)|(ARM64)")
@@ -388,28 +410,28 @@ else() # macOS and Linux
388410
)
389411

390412
# Link against ONNX Runtime
391-
target_link_libraries(SerialPrograms PRIVATE onnxruntime)
413+
target_link_libraries(SerialProgramsLib PRIVATE onnxruntime)
392414
# Find OpenCV
393415
find_package(OpenCV REQUIRED HINTS "/usr/local/opt/opencv/lib/cmake/opencv4/")
394416
endif()
395417

396418
include_directories(${OpenCV_INCLUDE_DIRS})
397419
link_directories(${OpenCV_LIBRARY_DIRS})
398-
target_link_libraries(SerialPrograms PRIVATE ${OpenCV_LIBS})
420+
target_link_libraries(SerialProgramsLib PRIVATE ${OpenCV_LIBS})
399421

400422
#we hope to use our own Tesseract build in future so we can rid that dependency
401423
#but right now to run on Linux and Mac we need to use external Tesseract library
402424
if (UNIX_LINK_TESSERACT)
403425
find_package(PkgConfig REQUIRED)
404-
target_compile_definitions(SerialPrograms PRIVATE PA_TESSERACT UNIX_LINK_TESSERACT)
426+
target_compile_definitions(SerialProgramsLib PRIVATE PA_TESSERACT UNIX_LINK_TESSERACT)
405427
pkg_search_module(TESSERACT REQUIRED tesseract)
406428
pkg_search_module(LEPTONICA REQUIRED lept)
407429
include_directories(${TESSERACT_INCLUDE_DIRS})
408430
include_directories(${LEPTONICA_INCLUDE_DIRS})
409431
link_directories(${TESSERACT_LIBRARY_DIRS})
410432
link_directories(${LEPTONICA_LIBRARY_DIRS})
411-
target_link_libraries(SerialPrograms PRIVATE ${TESSERACT_LINK_LIBRARIES})
412-
target_link_libraries(SerialPrograms PRIVATE ${LEPTONICA_LINK_LIBRARIES})
433+
target_link_libraries(SerialProgramsLib PRIVATE ${TESSERACT_LINK_LIBRARIES})
434+
target_link_libraries(SerialProgramsLib PRIVATE ${LEPTONICA_LINK_LIBRARIES})
413435
endif()
414436

415437
# enable dpp integration
@@ -420,46 +442,46 @@ else() # macOS and Linux
420442
INTERFACE_COMPILE_DEFINITIONS "PA_DPP"
421443
INTERFACE_LINK_LIBRARIES "-L/opt/homebrew/lib -lssl -lcrypto -lopus -lsodium -lz" # add dpp's deps
422444
)
423-
target_link_libraries(SerialPrograms PRIVATE libdpp)
445+
target_link_libraries(SerialProgramsLib PRIVATE libdpp)
424446
endif()
425447

426448
if (APPLE)
427449
# Add -Wno-c11-extensions to avoid clang gives
428450
# /usr/local/Cellar/opencv/4.5.5_3/include/opencv4/opencv2/core/mat.inl.hpp:2116:9: error: '_Atomic' is a C11 extension
429451
# when compiling OpenCV
430-
# target_compile_options(SerialPrograms PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-c11-extensions)
431-
target_compile_options(SerialPrograms PRIVATE -Wall -Wextra -Wpedantic -Werror -Wshorten-64-to-32)
452+
# target_compile_options(SerialProgramsLib PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-c11-extensions)
453+
target_compile_options(SerialProgramsLib PRIVATE -Wall -Wextra -Wpedantic -Werror -Wshorten-64-to-32)
432454
else()
433455
# Assume GCC
434-
target_compile_options(SerialPrograms PRIVATE -Wall -Wextra -Wpedantic -Werror -fno-strict-aliasing)
456+
target_compile_options(SerialProgramsLib PRIVATE -Wall -Wextra -Wpedantic -Werror -fno-strict-aliasing)
435457
endif()
436458

437459
# Set OS-specific flags
438460
if (WIN32)
439461
elseif (APPLE)
440462
# on macOS, need this framework to query OS API to control display sleep and system sleep behavior
441-
target_link_libraries(SerialPrograms PRIVATE "-framework IOKit -framework CoreFoundation")
463+
target_link_libraries(SerialProgramsLib PRIVATE "-framework IOKit -framework CoreFoundation")
442464
elseif(UNIX)
443465
endif()
444466

445467
IF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm")
446468
# Arm CPU
447469
# Run-time ISA dispatching
448-
target_compile_definitions(SerialPrograms PRIVATE PA_AutoDispatch_arm64_20_M1)
470+
target_compile_definitions(SerialProgramsLib PRIVATE PA_AutoDispatch_arm64_20_M1)
449471

450472
else()
451473
# Intel CPU
452-
target_compile_options(SerialPrograms PRIVATE -march=nehalem)
474+
target_compile_options(SerialProgramsLib PRIVATE -march=nehalem)
453475
set(ARCH_FLAGS_09_Nehalem -march=nehalem)
454476
set(ARCH_FLAGS_13_Haswell -march=haswell)
455477
set(ARCH_FLAGS_17_Skylake -march=skylake-avx512)
456478
set(ARCH_FLAGS_19_IceLake -march=icelake-client)
457479

458480
# Run-time ISA dispatching
459-
target_compile_definitions(SerialPrograms PRIVATE PA_AutoDispatch_x64_08_Nehalem)
460-
target_compile_definitions(SerialPrograms PRIVATE PA_AutoDispatch_x64_13_Haswell)
461-
target_compile_definitions(SerialPrograms PRIVATE PA_AutoDispatch_x64_17_Skylake)
462-
target_compile_definitions(SerialPrograms PRIVATE PA_AutoDispatch_x64_19_IceLake)
481+
target_compile_definitions(SerialProgramsLib PRIVATE PA_AutoDispatch_x64_08_Nehalem)
482+
target_compile_definitions(SerialProgramsLib PRIVATE PA_AutoDispatch_x64_13_Haswell)
483+
target_compile_definitions(SerialProgramsLib PRIVATE PA_AutoDispatch_x64_17_Skylake)
484+
target_compile_definitions(SerialProgramsLib PRIVATE PA_AutoDispatch_x64_19_IceLake)
463485

464486
endif()
465487
endif()

0 commit comments

Comments
 (0)