33# Cross-platform: Windows (Win32), Linux (Wayland), BSD (X11), macOS
44# ============================================================================
55cmake_minimum_required (VERSION 3.16 )
6- project (pcode-editor VERSION 0.1.0 LANGUAGES CXX )
6+ project (pcode-editor VERSION 0.2.95 LANGUAGES CXX )
77
88set (CMAKE_CXX_STANDARD 17)
99set (CMAKE_CXX_STANDARD_REQUIRED ON )
@@ -12,6 +12,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1212# Suppress deprecated function warnings (sprintf, strncpy, getenv, localtime)
1313add_compile_definitions (_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATION )
1414
15+ # Static runtime for portable builds (no VC++ redistributable needed)
16+ if (PLATFORM_WINDOWS)
17+ set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG :Debug >:Debug >" CACHE STRING "" FORCE )
18+ add_compile_options (/MT )
19+ endif ()
20+
1521# ============================================================================
1622# Platform Detection
1723# ============================================================================
@@ -30,80 +36,70 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
3036endif ()
3137
3238# ============================================================================
33- # Dependencies (fetched automatically via FetchContent)
39+ # Dependency Resolution: vcpkg preferred, FetchContent fallback
3440# ============================================================================
3541include (FetchContent )
3642
37- # GLFW — cross-platform window/input
38- FetchContent_Declare (
39- glfw
40- GIT_REPOSITORY https://github.com/glfw/glfw.git
41- GIT_TAG 3.4
42- )
43- set (GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE )
44- set (GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE )
45- set (GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )
46-
47- # Platform-specific GLFW configuration
48- if (PLATFORM_BSD)
49- # BSD: X11 only
50- set (GLFW_BUILD_WAYLAND OFF CACHE BOOL "" FORCE )
51- set (GLFW_BUILD_X11 ON CACHE BOOL "" FORCE )
52- message (STATUS "GLFW: Building with X11 support for BSD" )
53- elseif (PLATFORM_LINUX)
54- # Linux: Wayland by default
55- set (GLFW_BUILD_WAYLAND ON CACHE BOOL "" FORCE )
56- set (GLFW_BUILD_X11 OFF CACHE BOOL "" FORCE )
57- message (STATUS "GLFW: Building with Wayland support for Linux" )
43+ # Try vcpkg first (if installed)
44+ find_package (GLFW QUIET )
45+ find_package (imgui QUIET )
46+ find_package (nativefiledialog-extended QUIET )
47+
48+ # If vcpkg not found, use FetchContent (automatic download)
49+ if (NOT TARGET imgui::imgui)
50+ message (STATUS "Using FetchContent for dependencies (vcpkg not found)" )
51+
52+ # Dear ImGui — immediate mode GUI
53+ FetchContent_Declare (
54+ imgui
55+ GIT_REPOSITORY https://github.com/ocornut/imgui.git
56+ GIT_TAG docking # docking branch (stable, includes docking)
57+ )
58+ FetchContent_MakeAvailable (imgui)
5859endif ()
5960
60- FetchContent_MakeAvailable (glfw)
61+ if (NOT TARGET GLFW::GLFW)
62+ # GLFW — cross-platform window/input
63+ FetchContent_Declare (
64+ glfw
65+ GIT_REPOSITORY https://github.com/glfw/glfw.git
66+ GIT_TAG 3.4
67+ )
68+ set (GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE )
69+ set (GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE )
70+ set (GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )
71+
72+ # Platform-specific GLFW configuration
73+ if (PLATFORM_BSD)
74+ set (GLFW_BUILD_WAYLAND OFF CACHE BOOL "" FORCE )
75+ set (GLFW_BUILD_X11 ON CACHE BOOL "" FORCE )
76+ elseif (PLATFORM_LINUX)
77+ set (GLFW_BUILD_WAYLAND ON CACHE BOOL "" FORCE )
78+ set (GLFW_BUILD_X11 OFF CACHE BOOL "" FORCE )
79+ endif ()
80+
81+ FetchContent_MakeAvailable (glfw)
82+ endif ()
6183
62- # Dear ImGui — immediate mode GUI
63- FetchContent_Declare (
64- imgui
65- GIT_REPOSITORY https://github.com/ocornut/imgui.git
66- GIT_TAG docking # docking branch (stable, includes docking)
67- )
68- FetchContent_MakeAvailable (imgui)
84+ if (NOT TARGET nfd::nfd)
85+ # Native File Dialog — cross-platform native file picker
86+ FetchContent_Declare (
87+ nfd
88+ GIT_REPOSITORY https://github.com/btzy/nativefiledialog-extended.git
89+ GIT_TAG master
90+ )
91+ set (NFD_INSTALL ON CACHE BOOL "" FORCE )
92+ FetchContent_MakeAvailable (nfd)
93+ endif ()
6994
70- # ImGuiColorTextEdit — syntax highlighting, line numbers, minimap
95+ # ImGuiColorTextEdit — syntax highlighting (not in vcpkg, always FetchContent)
7196FetchContent_Declare (
7297 imgui_coloredit
7398 GIT_REPOSITORY https://github.com/BalazsJako/ImGuiColorTextEdit.git
7499 GIT_TAG master
75100)
76101FetchContent_MakeAvailable (imgui_coloredit)
77102
78- # Native File Dialog — cross-platform native file picker
79- FetchContent_Declare (
80- nfd
81- GIT_REPOSITORY https://github.com/btzy/nativefiledialog-extended.git
82- GIT_TAG master
83- )
84- set (NFD_INSTALL ON CACHE BOOL "" FORCE )
85- FetchContent_MakeAvailable (nfd)
86-
87- # ============================================================================
88- # Dear ImGui sources
89- # ============================================================================
90- set (IMGUI_SOURCES
91- ${imgui_SOURCE_DIR } /imgui.cpp
92- ${imgui_SOURCE_DIR } /imgui_demo.cpp
93- ${imgui_SOURCE_DIR } /imgui_draw.cpp
94- ${imgui_SOURCE_DIR } /imgui_tables.cpp
95- ${imgui_SOURCE_DIR } /imgui_widgets.cpp
96- ${imgui_SOURCE_DIR } /backends/imgui_impl_glfw.cpp
97- ${imgui_SOURCE_DIR } /backends/imgui_impl_opengl3.cpp
98- )
99-
100- # ============================================================================
101- # ImGuiColorTextEdit sources
102- # ============================================================================
103- set (TEXTEDIT_SOURCES
104- ${imgui_coloredit_SOURCE_DIR } /TextEditor.cpp
105- )
106-
107103# ============================================================================
108104# Application sources
109105# ============================================================================
@@ -117,31 +113,77 @@ set(APP_SOURCES
117113# Executable
118114# ============================================================================
119115add_executable (${PROJECT_NAME }
120- ${IMGUI_SOURCES}
121- ${TEXTEDIT_SOURCES}
122116 ${APP_SOURCES}
123117)
124118
119+ # Determine include directories based on dependency source (vcpkg or FetchContent)
120+ if (TARGET imgui::imgui)
121+ # vcpkg imgui - use imported targets
122+ target_link_libraries (${PROJECT_NAME } PRIVATE imgui::imgui )
123+ # imgui vcpkg includes imgui_impl_* in same dir, need to include backends differently
124+ if (imgui_FOUND)
125+ get_target_property (IMGUI_INCLUDE imgui::imgui INTERFACE_INCLUDE_DIRECTORIES )
126+ target_include_directories (${PROJECT_NAME } PRIVATE ${IMGUI_INCLUDE} )
127+ endif ()
128+ else ()
129+ # FetchContent imgui - use source dir
130+ target_include_directories (${PROJECT_NAME } PRIVATE ${imgui_SOURCE_DIR } )
131+ if (EXISTS ${imgui_SOURCE_DIR } /backends)
132+ target_include_directories (${PROJECT_NAME } PRIVATE ${imgui_SOURCE_DIR } /backends )
133+ endif ()
134+
135+ # Add imgui sources from FetchContent
136+ target_sources (${PROJECT_NAME } PRIVATE
137+ ${imgui_SOURCE_DIR } /imgui.cpp
138+ ${imgui_SOURCE_DIR } /imgui_demo.cpp
139+ ${imgui_SOURCE_DIR } /imgui_draw.cpp
140+ ${imgui_SOURCE_DIR } /imgui_tables.cpp
141+ ${imgui_SOURCE_DIR } /imgui_widgets.cpp
142+ ${imgui_SOURCE_DIR } /backends/imgui_impl_glfw.cpp
143+ ${imgui_SOURCE_DIR } /backends/imgui_impl_opengl3.cpp
144+ )
145+
146+ target_compile_definitions (${PROJECT_NAME } PRIVATE IMGUI_IMPL_OPENGL_LOADER_GLAD )
147+ endif ()
148+
125149target_include_directories (${PROJECT_NAME } PRIVATE
126- ${imgui_SOURCE_DIR }
127- ${imgui_SOURCE_DIR } /backends
128150 ${imgui_coloredit_SOURCE_DIR }
129151 ${nfd_SOURCE_DIR } /src/include
130- ${GLFW_INCLUDE_DIR}
152+ )
153+
154+ # Add ImGuiColorTextEdit source
155+ target_sources (${PROJECT_NAME } PRIVATE
156+ ${imgui_coloredit_SOURCE_DIR } /TextEditor.cpp
131157)
132158
133159# ============================================================================
134160# Platform-specific libraries and configuration
135161# ============================================================================
136162if (PLATFORM_WINDOWS)
137163 # Windows: Win32 API
138- target_link_libraries (${PROJECT_NAME } PRIVATE glfw opengl32 gdi32 nfd )
139- target_compile_definitions (${PROJECT_NAME } PRIVATE IMGUI_IMPL_OPENGL_LOADER_GLAD )
164+ if (TARGET GLFW::GLFW)
165+ target_link_libraries (${PROJECT_NAME } PRIVATE GLFW::GLFW )
166+ else ()
167+ target_link_libraries (${PROJECT_NAME } PRIVATE glfw )
168+ endif ()
169+ target_link_libraries (${PROJECT_NAME } PRIVATE opengl32 gdi32 )
170+
171+ if (TARGET nfd::nfd)
172+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd::nfd )
173+ else ()
174+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd )
175+ endif ()
140176 message (STATUS "Linking for Windows (Win32)" )
141177
142178elseif (PLATFORM_MACOS)
143179 # macOS: Cocoa + OpenGL
144- target_link_libraries (${PROJECT_NAME } PRIVATE glfw "-framework OpenGL" "-framework Cocoa" "-framework IOKit" "-framework CoreVideo" nfd )
180+ if (TARGET GLFW::GLFW)
181+ target_link_libraries (${PROJECT_NAME } PRIVATE GLFW::GLFW )
182+ else ()
183+ target_link_libraries (${PROJECT_NAME } PRIVATE glfw )
184+ endif ()
185+ target_link_libraries (${PROJECT_NAME } PRIVATE "-framework OpenGL" "-framework Cocoa" "-framework IOKit" "-framework CoreVideo" )
186+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd::nfd )
145187 message (STATUS "Linking for macOS" )
146188
147189elseif (PLATFORM_LINUX)
@@ -151,7 +193,18 @@ elseif(PLATFORM_LINUX)
151193 pkg_check_modules (WAYLAND REQUIRED wayland-client wayland-cursor wayland-egl xkbcommon )
152194 pkg_check_modules (GTK3 REQUIRED gtk+-3.0 )
153195
154- target_link_libraries (${PROJECT_NAME } PRIVATE glfw ${OPENGL_LIBRARIES} dl nfd ${GTK3_LIBRARIES} )
196+ target_link_libraries (${PROJECT_NAME } PRIVATE ${OPENGL_LIBRARIES} dl )
197+ if (TARGET GLFW::GLFW)
198+ target_link_libraries (${PROJECT_NAME } PRIVATE GLFW::GLFW )
199+ else ()
200+ target_link_libraries (${PROJECT_NAME } PRIVATE glfw )
201+ endif ()
202+ if (TARGET nfd::nfd)
203+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd::nfd )
204+ else ()
205+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd )
206+ endif ()
207+ target_link_libraries (${PROJECT_NAME } PRIVATE ${GTK3_LIBRARIES} )
155208 target_include_directories (${PROJECT_NAME } PRIVATE ${GTK3_INCLUDE_DIRS} ${WAYLAND_INCLUDE_DIRS} )
156209 target_compile_definitions (${PROJECT_NAME } PRIVATE IMGUI_IMPL_OPENGL_LOADER_GLAD )
157210 message (STATUS "Linking for Linux (Wayland)" )
@@ -163,9 +216,19 @@ elseif(PLATFORM_BSD)
163216 find_package (PkgConfig REQUIRED )
164217 pkg_check_modules (GTK3 REQUIRED gtk+-3.0 )
165218
166- # Add /usr/local/lib to library search path (common on BSD)
167219 target_link_directories (${PROJECT_NAME } PRIVATE /usr/local/lib )
168- target_link_libraries (${PROJECT_NAME } PRIVATE glfw ${OPENGL_LIBRARIES} dl nfd ${GTK3_LIBRARIES} )
220+ target_link_libraries (${PROJECT_NAME } PRIVATE ${OPENGL_LIBRARIES} dl )
221+ if (TARGET GLFW::GLFW)
222+ target_link_libraries (${PROJECT_NAME } PRIVATE GLFW::GLFW )
223+ else ()
224+ target_link_libraries (${PROJECT_NAME } PRIVATE glfw )
225+ endif ()
226+ if (TARGET nfd::nfd)
227+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd::nfd )
228+ else ()
229+ target_link_libraries (${PROJECT_NAME } PRIVATE nfd )
230+ endif ()
231+ target_link_libraries (${PROJECT_NAME } PRIVATE ${GTK3_LIBRARIES} )
169232 target_include_directories (${PROJECT_NAME } PRIVATE ${X11_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS} )
170233 target_compile_definitions (${PROJECT_NAME } PRIVATE IMGUI_IMPL_OPENGL_LOADER_GLAD )
171234 message (STATUS "Linking for BSD (X11)" )
0 commit comments