Skip to content

Commit 7d9b828

Browse files
committed
Update
1 parent 813f456 commit 7d9b828

6 files changed

Lines changed: 1012 additions & 42 deletions

File tree

examples/barcode-scanner/CMakeLists.txt

Lines changed: 195 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,93 @@ project(CameraProject VERSION 1.0 LANGUAGES CXX)
77
set(CMAKE_CXX_STANDARD 17)
88
set(CMAKE_CXX_STANDARD_REQUIRED True)
99

10+
# Build type
11+
if(NOT CMAKE_BUILD_TYPE)
12+
set(CMAKE_BUILD_TYPE Release)
13+
endif()
14+
15+
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
16+
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
17+
1018
# Define include directories
1119
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
1220

13-
# Define source files for the Camera library
14-
if (WIN32)
15-
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
21+
# Platform detection
22+
if(WIN32)
23+
set(PLATFORM_NAME "windows")
24+
elseif(APPLE)
25+
set(PLATFORM_NAME "macos")
26+
elseif(UNIX)
27+
set(PLATFORM_NAME "linux")
28+
else()
29+
set(PLATFORM_NAME "unknown")
30+
endif()
31+
32+
# Architecture detection
33+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
34+
set(ARCH_NAME "x86_64")
35+
else()
36+
set(ARCH_NAME "x86")
37+
endif()
38+
39+
message(STATUS "Target platform: ${PLATFORM_NAME}-${ARCH_NAME}")
40+
41+
# Compiler-specific settings
42+
if(MSVC)
43+
# Set runtime library for Windows
44+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
1645
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebug")
1746
else()
1847
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
1948
endif()
49+
50+
# Enable parallel compilation
51+
add_compile_options(/MP)
52+
53+
# Disable specific warnings
54+
add_compile_options(/wd4251 /wd4275)
55+
56+
# Enable UTF-8 encoding
57+
add_compile_options(/utf-8)
58+
endif()
2059

60+
# Define source files for the Camera library based on platform
61+
if (WIN32)
2162
set(LIBRARY_SOURCES
2263
src/CameraWindows.cpp
2364
src/CameraPreviewWindows.cpp
2465
)
66+
message(STATUS "Building for Windows platform")
2567
elseif (UNIX AND NOT APPLE)
2668
set(LIBRARY_SOURCES
2769
src/CameraLinux.cpp
2870
src/CameraPreviewLinux.cpp
2971
)
72+
message(STATUS "Building for Linux platform")
3073
elseif (APPLE)
74+
# Support universal binaries on macOS
3175
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
76+
3277
# Ensure that Objective-C++ source files are compiled as Objective-C++
3378
set(LIBRARY_SOURCES
3479
src/CameraMacOS.mm
3580
src/CameraPreviewMacOS.mm
3681
)
3782
set_source_files_properties(src/CameraMacOS.mm src/CameraPreviewMacOS.mm PROPERTIES COMPILE_FLAGS "-x objective-c++")
3883

39-
# Set main.cpp to be treated as Objective-C++
84+
# Set main.cpp to be treated as Objective-C++ for macOS
4085
set_source_files_properties(src/main.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
86+
87+
message(STATUS "Building for macOS platform with architectures: ${CMAKE_OSX_ARCHITECTURES}")
4188
endif()
4289

4390
# Add JNI wrapper source (common for all platforms)
4491
list(APPEND LIBRARY_SOURCES
4592
src/LiteCamJNI.cpp
4693
)
4794

95+
message(STATUS "Library sources: ${LIBRARY_SOURCES}")
96+
4897
# Define source files for the executable
4998
set(EXECUTABLE_SOURCES
5099
src/main.cpp
@@ -53,63 +102,197 @@ set(EXECUTABLE_SOURCES
53102
# Add the Camera shared library
54103
add_library(litecam SHARED ${LIBRARY_SOURCES})
55104

105+
# Set library properties
106+
set_target_properties(litecam PROPERTIES
107+
VERSION ${PROJECT_VERSION}
108+
SOVERSION ${PROJECT_VERSION_MAJOR}
109+
OUTPUT_NAME "litecam"
110+
)
111+
112+
# Platform-specific library naming
113+
if(WIN32)
114+
set_target_properties(litecam PROPERTIES
115+
PREFIX ""
116+
SUFFIX ".dll"
117+
)
118+
elseif(APPLE)
119+
set_target_properties(litecam PROPERTIES
120+
PREFIX "lib"
121+
SUFFIX ".dylib"
122+
)
123+
else()
124+
set_target_properties(litecam PROPERTIES
125+
PREFIX "lib"
126+
SUFFIX ".so"
127+
)
128+
endif()
129+
56130
# Set include directories for the Camera library
57-
target_include_directories(litecam PUBLIC ${INCLUDE_DIR})
131+
target_include_directories(litecam PUBLIC
132+
$<BUILD_INTERFACE:${INCLUDE_DIR}>
133+
$<INSTALL_INTERFACE:include>
134+
)
58135

59136
# Define the CAMERA_EXPORTS macro for the shared library
60-
target_compile_definitions(litecam PRIVATE CAMERA_EXPORTS)
137+
target_compile_definitions(litecam PRIVATE
138+
CAMERA_EXPORTS
139+
LITECAM_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
140+
LITECAM_VERSION_MINOR=${PROJECT_VERSION_MINOR}
141+
LITECAM_VERSION_PATCH=${PROJECT_VERSION_PATCH}
142+
)
61143

62144
# Platform-specific dependencies for the Camera library
63145
if (UNIX AND NOT APPLE)
146+
# Linux dependencies
64147
find_package(X11 REQUIRED)
148+
find_package(PkgConfig REQUIRED)
149+
150+
# Check for Video4Linux2
151+
pkg_check_modules(V4L2 libv4l2)
152+
65153
if (X11_FOUND)
66154
target_include_directories(litecam PUBLIC ${X11_INCLUDE_DIR})
67155
target_link_libraries(litecam PRIVATE ${X11_LIBRARIES} pthread)
156+
message(STATUS "Found X11: ${X11_LIBRARIES}")
157+
endif()
158+
159+
if (V4L2_FOUND)
160+
target_include_directories(litecam PRIVATE ${V4L2_INCLUDE_DIRS})
161+
target_link_libraries(litecam PRIVATE ${V4L2_LIBRARIES})
162+
message(STATUS "Found Video4Linux2: ${V4L2_LIBRARIES}")
163+
else()
164+
message(WARNING "Video4Linux2 not found - camera functionality may be limited")
68165
endif()
166+
69167
elseif (APPLE)
168+
# macOS dependencies
70169
find_library(COCOA_LIBRARY Cocoa REQUIRED)
71170
find_library(AVFOUNDATION_LIBRARY AVFoundation REQUIRED)
72171
find_library(COREMEDIA_LIBRARY CoreMedia REQUIRED)
73172
find_library(COREVIDEO_LIBRARY CoreVideo REQUIRED)
74-
find_library(OBJC_LIBRARY objc REQUIRED) # Add the Objective-C runtime library
173+
find_library(OBJC_LIBRARY objc REQUIRED)
75174

76175
target_link_libraries(litecam PRIVATE
77176
${COCOA_LIBRARY}
78177
${AVFOUNDATION_LIBRARY}
79178
${COREMEDIA_LIBRARY}
80179
${COREVIDEO_LIBRARY}
81-
${OBJC_LIBRARY} # Link the Objective-C runtime
180+
${OBJC_LIBRARY}
82181
)
182+
183+
message(STATUS "Found macOS frameworks: Cocoa, AVFoundation, CoreMedia, CoreVideo")
184+
83185
elseif (WIN32)
84-
target_link_libraries(litecam PRIVATE ole32 uuid mfplat mf mfreadwrite mfuuid)
186+
# Windows dependencies
187+
target_link_libraries(litecam PRIVATE
188+
ole32
189+
uuid
190+
mfplat
191+
mf
192+
mfreadwrite
193+
mfuuid
194+
)
195+
196+
message(STATUS "Linking Windows Media Foundation libraries")
85197
endif()
86198

87-
# JNI support
199+
# JNI support - enhanced detection
88200
find_package(JNI)
89201
if (JNI_FOUND)
90202
message(STATUS "JNI found: ${JNI_INCLUDE_DIRS}")
91203
target_include_directories(litecam PRIVATE ${JNI_INCLUDE_DIRS})
92204
target_compile_definitions(litecam PRIVATE LITECAM_JNI_ENABLED)
205+
206+
# Add JNI libraries on some platforms
207+
if(WIN32)
208+
# Windows doesn't typically need to link JNI libraries
209+
elseif(APPLE)
210+
# macOS typically has JNI in the framework
211+
else()
212+
# Linux might need explicit JNI library linking
213+
if(JNI_LIBRARIES)
214+
target_link_libraries(litecam PRIVATE ${JNI_LIBRARIES})
215+
endif()
216+
endif()
93217
else()
94218
message(WARNING "JNI not found; Java wrapper native methods won't be available.")
219+
message(WARNING "Set JAVA_HOME environment variable or install JDK to enable JNI support.")
95220
endif()
96221

222+
# Optional: Add position independent code for shared library
223+
set_property(TARGET litecam PROPERTY POSITION_INDEPENDENT_CODE ON)
224+
97225
# Add the camera_capture executable
98226
add_executable(camera_capture ${EXECUTABLE_SOURCES})
99227

228+
# Set executable properties
229+
set_target_properties(camera_capture PROPERTIES
230+
OUTPUT_NAME "camera_capture"
231+
)
232+
100233
# Link the Camera library to the executable
101234
target_link_libraries(camera_capture PRIVATE litecam)
102235

103236
# Include the shared library's headers in the executable
104237
target_include_directories(camera_capture PRIVATE ${INCLUDE_DIR})
105238

106-
# For macOS, link against the Cocoa framework
239+
# For macOS, link against the frameworks for the executable too
107240
if (APPLE)
108241
target_link_libraries(camera_capture PRIVATE
109242
${COCOA_LIBRARY}
110243
${AVFOUNDATION_LIBRARY}
111244
${COREMEDIA_LIBRARY}
112245
${COREVIDEO_LIBRARY}
113-
${OBJC_LIBRARY} # Link the Objective-C runtime
246+
${OBJC_LIBRARY}
114247
)
115248
endif()
249+
250+
# Installation rules (optional)
251+
install(TARGETS litecam camera_capture
252+
EXPORT CameraProjectTargets
253+
LIBRARY DESTINATION lib
254+
ARCHIVE DESTINATION lib
255+
RUNTIME DESTINATION bin
256+
INCLUDES DESTINATION include
257+
)
258+
259+
install(DIRECTORY ${INCLUDE_DIR}/
260+
DESTINATION include
261+
FILES_MATCHING PATTERN "*.h"
262+
)
263+
264+
# Export targets for find_package support
265+
install(EXPORT CameraProjectTargets
266+
FILE CameraProjectTargets.cmake
267+
NAMESPACE CameraProject::
268+
DESTINATION lib/cmake/CameraProject
269+
)
270+
271+
# Generate and install package config files
272+
include(CMakePackageConfigHelpers)
273+
274+
configure_package_config_file(
275+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/CameraProjectConfig.cmake.in"
276+
"${CMAKE_CURRENT_BINARY_DIR}/CameraProjectConfig.cmake"
277+
INSTALL_DESTINATION lib/cmake/CameraProject
278+
)
279+
280+
write_basic_package_version_file(
281+
"${CMAKE_CURRENT_BINARY_DIR}/CameraProjectConfigVersion.cmake"
282+
VERSION ${PROJECT_VERSION}
283+
COMPATIBILITY SameMajorVersion
284+
)
285+
286+
# Summary
287+
message(STATUS "=== Configuration Summary ===")
288+
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
289+
message(STATUS "Platform: ${PLATFORM_NAME}-${ARCH_NAME}")
290+
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
291+
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
292+
message(STATUS "JNI support: ${JNI_FOUND}")
293+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
294+
message(STATUS "Optimization: Enabled")
295+
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
296+
message(STATUS "Debug info: Enabled")
297+
endif()
298+
message(STATUS "===============================")

0 commit comments

Comments
 (0)