33# SPDX-License-Identifier: GPL-3.0-or-later
44# --------------------------------------------------------------
55# Build File : Makefile
6- # File Authors : Aoran Zeng <[email protected] >7- # Contributors : Yangmoooo <[email protected] >8- # | sanchuanhehe <[email protected] >9- # |
6+ # File Authors : Aoran Zeng <[email protected] >7+ # Contributors : Yangmoooo <[email protected] >8+ # | sanchuanhehe <[email protected] >9+ # | Mikachu2333 <[email protected] >10+ # |
1011# Created On : <2023-08-28>
11- # Last Modified : <2025-07-22 >
12+ # Last Modified : <2025-10-11 >
1213#
1314# 请阅读 ./doc/01-开发与构建.md 来使用
1415# --------------------------------------------------------------
@@ -18,26 +19,33 @@ On-Linux = 0
1819On-Windows = 0
1920On-macOS = 0
2021
21- ifeq ($(shell uname) , Linux)
22- On-Linux = 1
23- endif
24-
25- ifeq ($(shell uname) , Darwin)
26- On-macOS = 1
22+ # Windows 会定义 OS 或 ComSpec 环境变量
23+ ifdef ComSpec
24+ On-Windows = 1
25+ else ifdef OS
26+ ifeq ($(OS), Windows_NT)
27+ On-Windows = 1
28+ endif
2729endif
2830
29- # 只有Windows会定义$(OS)变量
30- ifeq ($(OS ) , Windows_NT)
31- On-Windows = 1
31+ # 只在非 Windows 环境下调用 uname
32+ ifneq ($(On-Windows ) , 1)
33+ UNAME_S := $(shell uname 2>/dev/null || echo unknown)
34+ ifeq ($(UNAME_S), Linux)
35+ On-Linux = 1
36+ endif
37+ ifeq ($(UNAME_S), Darwin)
38+ On-macOS = 1
39+ endif
3240endif
3341# =====================================
3442
3543
3644
3745# ======== Default Tooling ============
3846ifeq ($(On-Windows ) , 1)
39- # MSYS2 环境
40- CC = cc
47+ # Windows 环境 - 使用 gcc
48+ CC = gcc
4149else ifeq ($(On-macOS), 1)
4250 CC = clang
4351else
@@ -142,13 +150,25 @@ build-in-debug-mode:
142150build-in-release-mode : CFLAGS += $(CFLAGS_optimization )
143151build-in-release-mode :
144152 @echo Starting: Build in RELEASE mode: \' $(CC ) \' $(CFLAGS ) -o $(ReleaseMode-Target-Name )
153+ ifeq ($(On-Windows ) , 1)
154+ @if exist lib\chsrc.res del /Q lib\chsrc.res 2>nul
155+ @windres lib\win_res.rc -O coff -o lib\chsrc.res -Iinclude -Ilib
156+ @$(CC) src/chsrc-main.c lib/chsrc.res $(CFLAGS) $(_C_Warning_Flags) -o $(ReleaseMode-Target-Name)
157+ @del /Q lib\chsrc.res 2>nul
158+ else
145159 @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(ReleaseMode-Target-Name)
160+ endif
146161 @echo Finished: Build in RELEASE mode
147162
148163# CI release mode 的配置在该文件上方
149164build-in-ci-release-mode :
150165 @echo Starting: Build in CI-RELEASE mode: \' $(CC ) \' $(CFLAGS ) -o $(CIReleaseMode-Target-Name )
166+ ifeq ($(On-Windows ) , 1)
167+ @windres lib\win_res.rc -O coff -o lib\chsrc.res -Iinclude -Ilib
168+ @$(CC) src/chsrc-main.c lib/chsrc.res $(CFLAGS) $(_C_Warning_Flags) -o $(CIReleaseMode-Target-Name)
169+ else
151170 @$(CC) src/chsrc-main.c $(CFLAGS) $(_C_Warning_Flags) -o $(CIReleaseMode-Target-Name)
171+ endif
152172 @echo Finished: Build in CI-RELEASE mode
153173
154174# 永远重新编译
@@ -163,6 +183,10 @@ test-make-env:
163183 @echo " On-macOS: $( On-macOS) "
164184 @echo " CC: $( CC) "
165185 @echo " CFLAGS: $( CFLAGS) "
186+ ifeq ($(On-Windows ) , 1)
187+ @echo "USER: $(USERNAME)"
188+ @echo "PWD: $(CURDIR)"
189+ else
166190 @echo "USER: $$(whoami)"
167191 @echo "PWD: $(shell pwd)"
168192 @echo "UID: $$(id -u)"
@@ -173,17 +197,28 @@ test-make-env:
173197 else \
174198 echo "HOME: $(HOME)"; \
175199 fi
200+ endif
176201
177202# 这两个测试文件都用 DEBUG mode
178203test-xy : CFLAGS += $(CFLAGS_debug )
179204test-xy :
205+ ifeq ($(On-Windows ) , 1)
206+ @$(CC) test/xy.c $(CFLAGS) -o xy.exe
207+ @xy.exe
208+ else
180209 @$(CC) test/xy.c $(CFLAGS) -o xy
181210 @./xy
211+ endif
182212
183213test-fw : CFLAGS += $(CFLAGS_debug )
184214test-fw :
215+ ifeq ($(On-Windows ) , 1)
216+ @$(CC) test/fw.c $(CFLAGS) -o fw.exe
217+ @fw.exe
218+ else
185219 @$(CC) test/fw.c $(CFLAGS) -o fw
186220 @./fw
221+ endif
187222
188223check : test
189224
@@ -195,15 +230,25 @@ test-cli: $(DevMode-Target-Name)
195230 @perl ./test/cli.pl
196231
197232clean :
233+ ifeq ($(On-Windows ) , 1)
234+ -@if exist *.exe del /Q *.exe 2>nul
235+ -@if exist xy.exe del /Q xy.exe 2>nul
236+ -@if exist fw.exe del /Q fw.exe 2>nul
237+ -@if exist README.md.bak* del /Q README.md.bak* 2>nul
238+ -@if exist chsrc.exe del /Q chsrc.exe 2>nul
239+ -@if exist chsrc-debug.exe del /Q chsrc-debug.exe 2>nul
240+ -@if exist chsrc-release.exe del /Q chsrc-release.exe 2>nul
241+ -@if exist chsrc-ci-release.exe del /Q chsrc-ci-release.exe 2>nul
242+ else
198243 -@rm *.exe 2>/dev/null
199244 -@rm xy 2>/dev/null
200245 -@rm fw 2>/dev/null
201246 -@rm README.md.bak* 2>/dev/null
202-
203247 -@rm chsrc 2>/dev/null
204248 -@rm chsrc-debug 2>/dev/null
205249 -@rm chsrc-release 2>/dev/null
206250 -@rm chsrc-ci-release 2>/dev/null
251+ endif
207252
208253# -include pkg/deb/Makefile # 不这么做,因为 pkg/deb/Makefile 需要在 pkg/deb 目录下执行
209254# 保持动词在前的任务名风格
0 commit comments