-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
1900 lines (1570 loc) · 57 KB
/
CMakeLists.txt
File metadata and controls
1900 lines (1570 loc) · 57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: BSD-2-Clause
cmake_minimum_required(VERSION 3.22)
#
# Project name and base version.
#
# Note: the version here is the one currently *under development*. For released
# versions, checkout GIT tags like "release-0.1.1".
#
project(tilck VERSION 0.1.4 LANGUAGES C CXX ASM)
include(CheckCCompilerFlag)
include(ExternalProject)
set(GLOB_CONF_DEP CONFIGURE_DEPENDS)
include(other/cmake/utils.cmake)
include(other/cmake/errors.cmake)
# Truncate the tilck_option() JSONL sidecar exactly once per configure,
# before any sub-project re-includes kernel_options.cmake (or anything
# else that calls tilck_option()). The macro itself guards against
# duplicate emission when called from multiple sub-projects.
tilck_init_options_sidecar()
# Include kernel_options.cmake at the ROOT level so tilck_option()
# calls run during the top-level configure — the sub-projects (kernel,
# gtests) are ExternalProjects configured at build time, so their own
# include() of the same file runs too late to populate the sidecar
# before the validator runs at the end of this file. set(... CACHE ...)
# is idempotent, so re-inclusion in sub-projects is a no-op for cache
# vars; tilck_option()'s GLOBAL emission guard prevents duplicate
# JSONL records within any single configure run.
include(other/cmake/kernel_options.cmake)
set(BUILD_SHARED_LIBRARIES off) # disable shared libs globally
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS) # disable -rdynamic globally
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS) # disable -rdynamic globally
set(CMAKE_VERBOSE_MAKEFILE off)
set(PROJ_ROOT ${CMAKE_SOURCE_DIR})
set(BUILD_DIR ${CMAKE_BINARY_DIR})
set(DEFAULT_TARGET_ARCH "i386")
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set(HOST_ARCH "x86_64")
set(HOST_ARCH_BITS 64)
set(HOST_ARCH_FAMILY "generic_x86")
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64")
set(HOST_ARCH "x86_64")
set(HOST_ARCH_BITS 64)
set(HOST_ARCH_FAMILY "generic_x86")
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
set(HOST_ARCH "i386")
set(HOST_ARCH_BITS 32)
set(HOST_ARCH_FAMILY "generic_x86")
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i386")
set(HOST_ARCH "i386")
set(HOST_ARCH_BITS 32)
set(HOST_ARCH_FAMILY "generic_x86")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
set(HOST_ARCH "aarch64")
set(HOST_ARCH_BITS 64)
set(HOST_ARCH_FAMILY "arm")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set(HOST_ARCH "aarch64")
set(HOST_ARCH_BITS 64)
set(HOST_ARCH_FAMILY "arm")
endif()
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
set(GNU_WRAP_DIR ${CMAKE_SOURCE_DIR}/scripts/gnu-wrap)
set(DD ${GNU_WRAP_DIR}/dd)
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
set(GNU_WRAP_DIR ${CMAKE_SOURCE_DIR}/scripts/gnu-wrap)
set(DD ${GNU_WRAP_DIR}/dd)
else()
set(DD "dd")
endif()
# Host environment detection.
#
# HOST_OS = "linux" | "macos" | "freebsd"
# HOST_DISTRO = "ubuntu-22.04" | "macos-14.3" | "freebsd-14.0" | ...
# HOST_CC = "gcc-13.3.0" | "clang-14.0.0" | ...
#
# These values are used to construct the toolchain4 host tool paths
# (HOST_DIR_PORTABLE / HOST_DIR below). They MUST stay in sync with the
# Ruby pkgmgr implementation in scripts/pkgmgr/early_logic.rb (InitOnly
# module: get_host_os, get_host_distro, get_host_cc), otherwise CMake
# will look for host tools in a different subtree than where the
# package manager installed them.
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
set(HOST_OS "linux")
if (NOT EXISTS "/etc/os-release")
message(FATAL_ERROR "/etc/os-release not found: cannot detect distro")
endif()
file(STRINGS "/etc/os-release" _id_line REGEX "^ID=")
file(STRINGS "/etc/os-release" _ver_line REGEX "^VERSION_ID=")
string(REPLACE "ID=" "" _id "${_id_line}")
string(REPLACE "VERSION_ID=" "" _ver "${_ver_line}")
string(REPLACE "\"" "" _id "${_id}")
string(REPLACE "\"" "" _ver "${_ver}")
if ("${_id}" STREQUAL "" OR "${_ver}" STREQUAL "")
message(FATAL_ERROR "/etc/os-release is missing ID or VERSION_ID")
endif()
set(HOST_DISTRO "${_id}-${_ver}")
unset(_id_line)
unset(_ver_line)
unset(_id)
unset(_ver)
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
set(HOST_OS "macos")
execute_process(
COMMAND sw_vers -productVersion
OUTPUT_VARIABLE _mac_ver
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if ("${_mac_ver}" STREQUAL "")
message(FATAL_ERROR "sw_vers -productVersion returned nothing")
endif()
# Use only major.minor (e.g. "26.4") so a minor-patch bump like
# 26.4 -> 26.4.1 doesn't orphan the distro subtree.
string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _mac_ver "${_mac_ver}")
set(HOST_DISTRO "macos-${_mac_ver}")
unset(_mac_ver)
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
set(HOST_OS "freebsd")
execute_process(
COMMAND uname -r
OUTPUT_VARIABLE _fbsd_ver
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^([^-]+).*$" "\\1" _fbsd_ver "${_fbsd_ver}")
if ("${_fbsd_ver}" STREQUAL "")
message(FATAL_ERROR "uname -r returned nothing")
endif()
set(HOST_DISTRO "freebsd-${_fbsd_ver}")
unset(_fbsd_ver)
else()
message(FATAL_ERROR "Unsupported host OS: ${CMAKE_HOST_SYSTEM_NAME}")
endif()
# HOST_CC: family+version of the host C compiler detected by project().
# Must match the Ruby pkgmgr's get_host_cc, which reads `$CC -v` output.
#
# Apple Clang reports a 4-component version to CMake (e.g. 21.0.0.21000099)
# while `clang -v` says "Apple clang version 21.0.0 ...". The Ruby pkgmgr
# parses the latter, so truncate to major.minor.patch here to stay in sync.
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
string(REGEX REPLACE
"^([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1"
_clang_ver "${CMAKE_C_COMPILER_VERSION}"
)
set(HOST_CC "clang-${_clang_ver}")
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(HOST_CC "gcc-${CMAKE_C_COMPILER_VERSION}")
else()
message(
FATAL_ERROR
"Unsupported host C compiler: ${CMAKE_C_COMPILER_ID}"
)
endif()
# BEGIN [Environment-controlled "sticky" cache variables]
# Main variables
define_env_cache_str_var(TCROOT_PARENT ${CMAKE_SOURCE_DIR})
define_env_cache_str_var(TCROOT ${TCROOT_PARENT}/toolchain4)
define_env_cache_str_var(ARCH ${DEFAULT_TARGET_ARCH})
define_env_cache_bool_var(TEST_GCOV)
define_env_cache_bool_var(KERNEL_GCOV)
# Board defaults and stick variable
if (${ARCH} STREQUAL "riscv64")
set(ARCH_DEFAULT_BOARD "qemu-virt")
else()
set(ARCH_DEFAULT_BOARD "")
endif()
define_env_cache_str_var(BOARD "${ARCH_DEFAULT_BOARD}")
# Keep USE_SYSCC as a sticky env variable so that we can fail, if used.
define_env_cache_bool_var(USE_SYSCC)
# Debug cache variables
# We don't really need to cache the values of CC/CXX, but is useful to that
# in order to prevent user errors. Since CC/CXX affect the build only the first
# time CMake is run, it makes sense to check for the next runs whether the env
# vars CC/CXX changed (if set) by the user or an incorrect script. In case
# they are, we have to stop the build because their new value will be otherwise
# completely ignored. In order words, fail *early*.
define_env_cache_str_var(CC "<unset>")
define_env_cache_str_var(CXX "<unset>")
# END [Environment-controlled "sticky" cache variables]
# BEGIN [Setup of toolchain-related variables]
set(BUILD_SCRIPTS ${CMAKE_SOURCE_DIR}/scripts/build_scripts)
set(BUILD_APPS ${CMAKE_BINARY_DIR}/scripts/build_apps)
set(BTC_SCRIPT ${CMAKE_SOURCE_DIR}/scripts/build_toolchain)
set(COVERAGE_HTML_DIR ${CMAKE_BINARY_DIR}/coverage_html)
# Host-side install roots in the toolchain4 layout.
#
# HOST_DIR_PORTABLE — statically-linked, distro-agnostic host tools (cross
# compilers). Shared across distros and host compilers; a GCC_TC_VER
# bump does not invalidate extracted tarballs here.
#
# HOST_DIR — non-portable host tools (host_mtools, host_gtest) whose
# binaries or libraries depend on glibc/libstdc++ from a specific distro,
# built with a specific host compiler. Because C++ has no stable ABI, the
# host-compiler version is part of the path even for C-only tools that
# may pull in C++ host libs.
set(HOST_DIR_PORTABLE ${TCROOT}/host/${HOST_OS}-${HOST_ARCH}/portable)
set(HOST_DIR_DISTRO ${TCROOT}/host/${HOST_OS}-${HOST_ARCH}/${HOST_DISTRO})
set(HOST_DIR ${HOST_DIR_DISTRO}/${HOST_CC})
set(CMAKE_DIR ${CMAKE_SOURCE_DIR}/other/cmake)
file(RELATIVE_PATH BTC_SCRIPT_REL ${CMAKE_SOURCE_DIR} ${BTC_SCRIPT})
file(RELATIVE_PATH TCROOT_REL ${CMAKE_SOURCE_DIR} ${TCROOT})
if (NOT EXISTS ${TCROOT})
message(FATAL_ERROR "Toolchain not found: run ${BTC_SCRIPT_REL} first.")
endif()
# Set the GCC TC config directory
set(GCC_TC_CONF ${CMAKE_SOURCE_DIR}/other/gcc_tc_conf)
# Read the default GCC TC VER from the config file
file(READ ${GCC_TC_CONF}/${ARCH}/default_ver DEFAULT_GCC_TC_VER)
# Define our special "sticky" env. variable GCC_TC_VER
define_env_cache_str_var(GCC_TC_VER "${DEFAULT_GCC_TC_VER}")
#
# GCC_TC_VER check
#
file(READ ${GCC_TC_CONF}/${ARCH}/min_ver MIN_GCC_TC_VER)
if ("${GCC_TC_VER}" VERSION_LESS "${MIN_GCC_TC_VER}")
set (msg, "")
string(
CONCAT msg
"GCC_TC_VER (${GCC_TC_VER}) < "
"MIN_GCC_TC_VER (${MIN_GCC_TC_VER}).\n"
"This probably happened because the minimum GCC version has been bumped "
"by a source change. It's perfectly normal. Steps to fix:\n"
" 0. unset \$GCC_TC_VER # if set\n"
" 1. rm -rf ${CMAKE_BINARY_DIR}\n"
" 2. ${BTC_SCRIPT_REL} --clean\n"
" 3. ${BTC_SCRIPT_REL}\n"
" 4. rebuild"
)
message(FATAL_ERROR "${msg}")
unset(msg)
endif()
# END [Setup of toolchain-related variables]
if (USE_SYSCC)
message(FATAL_ERROR "USE_SYSCC is not supported anymore")
endif()
#
# Read all the VER_<PACKAGE>=<VALUE> pairs from pkg_versions
#
# Re-run CMake automatically when pkg_versions changes (e.g. version bump)
# so that the upgrade check below fires and VER_* variables are refreshed.
set_property(DIRECTORY APPEND PROPERTY
CMAKE_CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/other/pkg_versions)
file(STRINGS ${CMAKE_SOURCE_DIR}/other/pkg_versions PKG_VERSIONS)
foreach(LINE ${PKG_VERSIONS})
if(NOT "${LINE}" STREQUAL "" AND NOT "${LINE}" MATCHES "^#")
string(FIND "${LINE}" "=" EQUAL_POS)
if (EQUAL_POS EQUAL -1)
message(FATAL_ERROR "Broken key/value syntax in pkg_versions")
endif()
string(SUBSTRING "${LINE}" 0 ${EQUAL_POS} KEY)
math(EXPR VALUE_START_POS "${EQUAL_POS} + 1")
string(SUBSTRING "${LINE}" ${VALUE_START_POS} -1 VALUE)
set(${KEY} "${VALUE}")
list(APPEND PKG_VERSION_VARS ${KEY})
message(STATUS "${KEY} = '${VALUE}'")
endif()
endforeach()
unset(EQUAL_POS)
unset(KEY)
unset(LINE)
unset(VALUE)
unset(VALUE_START_POS)
#
# Lightweight check for package upgrades. Calls the Ruby pkgmgr directly
# (no bash wrapper, no system-package checks) so it adds <100ms to cmake.
# Exit code 0 = up to date, 2 = upgrades needed.
#
# The Ruby binary may live in the toolchain (downloaded/built by the bash
# bootstrap) or be the system Ruby (used when the system version >= 3.2).
# Try the toolchain path first, fall back to system `ruby`.
#
set(PKGMGR_RUBY "")
set(_tc_ruby
${TCROOT}/host/${HOST_OS}-${HOST_ARCH}/${HOST_DISTRO}/ruby/${VER_RUBY}/bin/ruby)
if (EXISTS ${_tc_ruby})
set(PKGMGR_RUBY ${_tc_ruby})
else()
find_program(PKGMGR_RUBY ruby)
endif()
unset(_tc_ruby)
set(PKGMGR_MAIN ${CMAKE_SOURCE_DIR}/scripts/pkgmgr/main.rb)
if (PKGMGR_RUBY AND EXISTS ${PKGMGR_MAIN})
execute_process(
COMMAND ${PKGMGR_RUBY} ${PKGMGR_MAIN} -q --check-for-updates
RESULT_VARIABLE _upgrade_rc
OUTPUT_VARIABLE _upgrade_out
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (_upgrade_rc EQUAL 2)
message(FATAL_ERROR
"\nSome installed toolchain packages need upgrading:\n"
" ${_upgrade_out}\n\n"
"Run: ${BTC_SCRIPT_REL} --upgrade\n")
endif()
unset(_upgrade_rc)
unset(_upgrade_out)
endif()
# Reset the default flags
set(CMAKE_C_FLAGS "")
set(CMAKE_C_FLAGS_INT "")
set(CMAKE_C_FLAGS_DEBUG "")
set(CMAKE_C_FLAGS_MINSIZEREL "")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "")
set(CMAKE_C_FLAGS_RELEASE "")
set(CMAKE_CXX_FLAGS "")
set(CMAKE_CXX_FLAGS_INT "")
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_MINSIZEREL "")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "")
set(CMAKE_CXX_FLAGS_RELEASE "")
# Compile options
# =====================================================================
# Shared options: exposed to the kernel + tests/unit sub-projects
# via the _krn_forward_args forwarding block below (any KRN_* OR any
# cache variable CMake considers a shared option). Migrated to
# tilck_option() so they show up in the generated run_config's menuconfig.
# =====================================================================
# --- Build type ---
#
# CATEGORY "." pins this to the very top of the menu (above every
# submenu). The backing variable is CMake's own CMAKE_BUILD_TYPE,
# already set by scripts/cmake_run; declaring it here just exposes it
# in the configurator. STRINGS must stay in sync with the build types
# handled in other/cmake/compiler_flags.cmake.
tilck_option(CMAKE_BUILD_TYPE
TYPE ENUM
CATEGORY "."
DEFAULT Debug
STRINGS Debug Release MinSizeRel RelWithDebInfo
HELP "Build type"
"CMake build type: optimization level (-ggdb is always on)."
"Debug -O0, RelWithDebInfo -O2, Release -O3, MinSizeRel -Os."
"Debug/RelWithDebInfo default DEBUG_CHECKS on, others off."
)
# --- Graphics ---
tilck_option(PREFERRED_GFX_MODE_W
TYPE UINT
CATEGORY "Misc"
DEFAULT 800
HELP "Preferred video mode (width, pixels)"
)
tilck_option(PREFERRED_GFX_MODE_H
TYPE UINT
CATEGORY "Misc"
DEFAULT 600
HELP "Preferred video mode (height, pixels)"
)
# --- Filesystems ---
tilck_option(FATPART_CLUSTER_SIZE
TYPE UINT
CATEGORY "Misc"
DEFAULT 8
HELP "FAT ramdisk cluster size (sectors)"
)
tilck_option(FAT_TEST_DIR
TYPE BOOL
CATEGORY "Misc"
DEFAULT OFF
HELP "Add a test dir with many files to the FAT32 initrd"
)
# --- Bootloader (arch-dependent) ---
if (${ARCH} STREQUAL "riscv64")
tilck_option(BOOTLOADER_U_BOOT
TYPE BOOL
CATEGORY "Bootloader"
DEFAULT ON
HELP "Build the U-Boot bootloader"
)
else()
tilck_option(BOOTLOADER_LEGACY
TYPE BOOL
CATEGORY "Bootloader"
DEFAULT ON
HELP "Build the legacy BIOS bootloader"
)
tilck_option(BOOTLOADER_EFI
TYPE BOOL
CATEGORY "Bootloader"
DEFAULT ON
HELP "Build the UEFI bootloader"
)
tilck_option(EFI_BOOTLOADER_DEBUG
TYPE BOOL
CATEGORY "Bootloader"
DEFAULT OFF
DEPENDS BOOTLOADER_EFI
HELP "Early DEBUG dialog in EFI bootloader"
"See docs/debugging.md. Only meaningful when the EFI"
"bootloader is being built."
)
endif()
tilck_option(BOOT_INTERACTIVE
TYPE BOOL
CATEGORY "Bootloader"
DEFAULT ON
HELP "Enable interactive bootloader commands"
)
tilck_option(BOOTLOADER_POISON_MEMORY
TYPE BOOL
CATEGORY "Bootloader"
DEFAULT OFF
HELP "Poison all available memory in the bootloader"
)
# --- Userapps ---
tilck_option(USERAPPS_busybox
TYPE BOOL
CATEGORY "Userapps"
DEFAULT ON
HELP "Include BusyBox in the image (recommended)"
)
tilck_option(TYPICAL_DEVEL_USERAPPS
TYPE BOOL
CATEGORY "Userapps"
DEFAULT ON
HELP "Include kernel-testing dev apps in the ramdisk"
)
tilck_option(USERAPPS_CFLAGS
TYPE STRING
CATEGORY "Userapps"
DEFAULT "-ggdb -Os -Wall -Wno-unused-parameter"
HELP "Build flags for C/C++ user programs"
)
# --- Kernel / Memory ---
tilck_option(KERNEL_64BIT_OFFT
TYPE BOOL
CATEGORY "Kernel Memory"
DEFAULT ON
HELP "Make off_t 64-bit even on 32-bit targets"
)
tilck_option(FORK_NO_COW
TYPE BOOL
CATEGORY "Kernel Memory"
DEFAULT OFF
HELP "fork(): full memory copy instead of copy-on-write"
)
tilck_option(MMAP_NO_COW
TYPE BOOL
CATEGORY "Kernel Memory"
DEFAULT OFF
HELP "mmap(): allocate real memory instead of zero-page + COW"
)
tilck_option(KRN32_LIN_VADDR
TYPE BOOL
CATEGORY "Kernel Memory"
DEFAULT ON
HELP "Place the 32-bit kernel at the default linear mapping"
)
# --- Kernel / Terminal ---
tilck_option(TTY_COUNT
TYPE UINT
CATEGORY "Kernel Terminal"
DEFAULT 2
HELP "Number of virtual TTYs"
)
tilck_option(TERM_BIG_SCROLL_BUF
TYPE BOOL
CATEGORY "Kernel Terminal"
DEFAULT OFF
HELP "Use a 4x larger scrollback buffer"
)
# KERNEL_SERCON vs SERIAL_CON_IN_VIDEO_MODE: the first makes the
# serial port the primary and ONLY console; the second enables an
# additional console on /dev/ttyS0 alongside the video console.
tilck_option(SERIAL_CON_IN_VIDEO_MODE
TYPE BOOL
CATEGORY "Kernel Terminal"
DEFAULT ON
HELP "Also open /dev/ttyS0 console in video mode"
)
tilck_option(KERNEL_SERCON
TYPE BOOL
CATEGORY "Kernel Terminal"
DEFAULT OFF
HELP "Use serial as the only console"
"Forces the kernel to boot with the serial console as the"
"primary (and only) console, even when a video console"
"module is available."
)
# --- Kernel / Debug ---
# DEBUG_CHECKS gates ASSERT() and all #if DEBUG_CHECKS code; with it off,
# NDEBUG is defined so ASSERTs don't fire (see compiler_flags.cmake).
# Default on for the debuggable build types, off for the production ones;
# it stays freely toggleable -- this only picks the initial default, which
# (as for any option) a cached value overrides on reconfigure.
set(_debug_checks_default OFF)
if (CMAKE_BUILD_TYPE MATCHES "^(Debug|RelWithDebInfo)$")
set(_debug_checks_default ON)
endif()
tilck_option(DEBUG_CHECKS
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT ${_debug_checks_default}
HELP "ASSERTs and DEBUG checks"
"Compile in ASSERT() checks and other debug-only code."
"With it off, NDEBUG is defined and ASSERTs never fire,"
"on Debug builds too. Defaults on for Debug and"
"RelWithDebInfo, off for Release and MinSizeRel."
)
tilck_option(KERNEL_SELFTESTS
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT ON
HELP "Compile-in the kernel's self-tests"
)
tilck_option(PANIC_SHOW_STACKTRACE
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT ON
HELP "Show the stacktrace on kernel panic"
)
tilck_option(PANIC_SHOW_REGS
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT OFF
HELP "Show register contents on kernel panic"
)
tilck_option(KERNEL_UBSAN
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT OFF
HELP "Enable UBSAN instrumentation in the kernel"
)
tilck_option(KERNEL_SAT
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT OFF
HELP "Enable the sched-alive thread at boot (CI runs)"
)
tilck_option(PS2_DO_SELFTEST
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT OFF
HELP "Run PS/2 controller selftests in init_kb()"
)
tilck_option(PS2_VERBOSE_DEBUG_LOG
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT OFF
HELP "Enable verbose PS/2 debug logging (dev-only)"
)
tilck_option(INIT_REPORT_PROC_EXIT
TYPE BOOL
CATEGORY "Kernel Debug"
DEFAULT OFF
HELP "Report child-process exits on init's TTY"
)
# --- Kernel / Misc ---
tilck_option(KERNEL_SYSCC
TYPE BOOL
CATEGORY "Kernel Misc"
DEFAULT OFF
HELP "Use system compiler instead of the toolchain's"
)
tilck_option(KERNEL_FORCE_TC_ISYSTEM
TYPE BOOL
CATEGORY "Kernel Misc"
DEFAULT OFF
HELP "Force toolchain sysroot with a system compiler (DANGEROUS)"
"Only meant for static-analysis builds. Brutally forces the"
"build system to use the toolchain sysroot for system"
"headers even when a system compiler is in use."
)
tilck_option(WCONV
TYPE BOOL
CATEGORY "Kernel Misc"
DEFAULT OFF
HELP "Compile with -Wconversion (clang only)"
)
list(
APPEND no_arch_modules_whitelist
console
tracing
sysfs
null
)
list(
APPEND disabled_modules_list
# e1000 driver still in development
e1000
)
if (${ARCH} STREQUAL "riscv64")
list(
APPEND disabled_modules_list
# The riscv compiler does not support the acpi module
acpi
)
endif()
file(GLOB modules ${GLOB_CONF_DEP} "${PROJ_ROOT}/modules/*")
foreach(modpath ${modules})
get_filename_component(modname ${modpath} NAME_WE)
if (NOT IS_DIRECTORY ${modpath})
continue()
endif()
list(APPEND modules_list ${modname})
list(FIND disabled_modules_list ${modname} _index)
if (${_index} GREATER -1)
set(_mod_default OFF)
else()
set(_mod_default ON)
endif()
# Read modules/<mod>/module_deps (one dep per line, plain module
# names) and convert to the MOD_<name>-prefixed DEPENDS list. The
# file is optional; most modules have no deps.
set(_mod_deps "")
set(_mod_deps_file "${PROJ_ROOT}/modules/${modname}/module_deps")
if (EXISTS ${_mod_deps_file})
file(STRINGS ${_mod_deps_file} _mod_raw_deps)
foreach(_dep_name ${_mod_raw_deps})
list(APPEND _mod_deps "MOD_${_dep_name}")
endforeach()
endif()
tilck_option(MOD_${modname}
TYPE BOOL
CATEGORY "Modules"
DEFAULT ${_mod_default}
DEPENDS ${_mod_deps}
HELP "Compile-in the ${modname} module"
)
unset(_mod_default)
unset(_mod_deps)
unset(_mod_deps_file)
# If the module ships an options.cmake, include it now. These
# files typically declare per-module tilck_option()s with
# CATEGORY "Modules" + DEPENDS MOD_<name>. They show up in
# mconf directly after MOD_<name> (options are emitted in the
# order their tilck_option() calls run) and are hidden when the
# module's compile-in toggle is off.
set(_mod_opts_file "${PROJ_ROOT}/modules/${modname}/options.cmake")
if (EXISTS ${_mod_opts_file})
include(${_mod_opts_file})
endif()
unset(_mod_opts_file)
set(MOD_HEADER_FILE ${PROJ_ROOT}/config/kernel/mod_${modname}.h)
if (NOT EXISTS ${MOD_HEADER_FILE})
message(FATAL_ERROR "Mod header ${MOD_HEADER_FILE} does not exist")
endif()
unset(MOD_HEADER_FILE)
endforeach()
# Create a list with the shared kernel opts, in order to dump it later.
# Kernel-private options are printed by the kernel CMake project.
# -------------------------------------------------------------------
list(
APPEND kernel_opts_list
# Various options
ARCH
BOARD
# Shared non-boolean options
TTY_COUNT
FATPART_CLUSTER_SIZE
PREFERRED_GFX_MODE_W
PREFERRED_GFX_MODE_H
# Shared boolean options ENABLED by default
PANIC_SHOW_STACKTRACE
DEBUG_CHECKS
BOOTLOADER_LEGACY
BOOTLOADER_EFI
BOOTLOADER_U_BOOT
BOOT_INTERACTIVE
SERIAL_CON_IN_VIDEO_MODE
KRN32_LIN_VADDR
USERAPPS_busybox
TYPICAL_DEVEL_USERAPPS
# Shared boolean options DISABLED by default
KERNEL_UBSAN
TERM_BIG_SCROLL_BUF
TEST_GCOV
KERNEL_GCOV
KERNEL_SYSCC
KERNEL_FORCE_TC_ISYSTEM
PANIC_SHOW_REGS
BOOTLOADER_POISON_MEMORY
WCONV
FAT_TEST_DIR
PS2_DO_SELFTEST
PS2_VERBOSE_DEBUG_LOG
INIT_REPORT_PROC_EXIT
EFI_BOOTLOADER_DEBUG
KERNEL_SERCON
KERNEL_SAT
)
###########################################################
if (${ARCH} STREQUAL "i386")
set(ARCH_SHORT "x86")
set(ARCH_FAMILY "generic_x86")
set(ARCH_ELF_NAME "elf32-i386")
set(ARCH_LD_OUTPUT "elf_i386")
set(ARCH_BFD "i386")
set(ARCH_GCC_TC "i686")
set(ARCH_GCC_FLAGS "-march=${ARCH_GCC_TC}")
set(ARCH_BITS 32)
# Fundamental kernel MM constants
set(BASE_VA 0xC0000000) # Better not touch!
set(KERNEL_PADDR 0x00100000) # Better not touch!
if (KRN32_LIN_VADDR)
set(LINEAR_MAPPING_MB 896) # Cannot be > 896 MB because of HiMem
set(KERNEL_BASE_VA ${BASE_VA})
set(KERNEL_VADDR 0xC0100000) # BASE_VA + KERNEL_PADDR
else()
set(LINEAR_MAPPING_MB 880) # Make room for the kernel and extra
set(KERNEL_BASE_VA 0xF7000000) # LINEAR_MAPPING_END
set(KERNEL_VADDR 0xF7100000) # LINEAR_MAPPING_END + KERNEL_PADDR
endif()
elseif (${ARCH} STREQUAL "x86_64")
set(ARCH_SHORT "x86_64")
set(ARCH_FAMILY "generic_x86")
set(ARCH_ELF_NAME "elf64-x86-64")
set(ARCH_LD_OUTPUT "elf_x86_64")
set(ARCH_BFD "i386:x86-64")
set(ARCH_GCC_TC "x86_64")
set(ARCH_GCC_FLAGS "")
set(ARCH_BITS 64)
# Fundamental kernel MM constants
set(BASE_VA 0x100000000000) # +16 TB
set(KERNEL_PADDR 0x00100000) # Better not touch (for now)
set(LINEAR_MAPPING_MB 4096) # Might be updated.
set(KERNEL_BASE_VA 0xFFFFFFFF80000000)
set(KERNEL_VADDR 0xFFFFFFFF80100000) # -2 GB + 1 MB
elseif (${ARCH} STREQUAL "riscv64")
set(ARCH_SHORT "riscv64")
set(ARCH_FAMILY "riscv")
set(ARCH_ELF_NAME "elf64-littleriscv")
set(ARCH_LD_OUTPUT "elf64lriscv")
set(ARCH_BFD "riscv:rv64")
set(ARCH_GCC_TC "riscv64")
set(ARCH_GCC_FLAGS "-march=rv64imafdc -mabi=lp64d -mcmodel=medany")
set(ARCH_GCC_FLAGS "${ARCH_GCC_FLAGS} -D__riscv64")
set(ARCH_BITS 64)
# Fundamental kernel MM constants
set(BASE_VA 0x2000000000) # +128 GB
if (KRN32_LIN_VADDR)
set(LINEAR_MAPPING_MB 896) # Cannot be > 896 MB
set(KERNEL_BASE_VA ${BASE_VA})
set(KERNEL_VADDR 0x2000200000) # BASE_VA + 2MB
else()
set(LINEAR_MAPPING_MB 896) # todo: Cannot be > 896 MB
set(KERNEL_BASE_VA 0xFFFFFFFF80000000) # -2 GB
set(KERNEL_VADDR 0xFFFFFFFF80200000) # -2 GB + 2MB
endif()
else()
message(FATAL_ERROR "Architecture '${ARCH}' not supported.")
endif()
message(STATUS "TCROOT: ${TCROOT}")
message(STATUS "GCC_TC_VER: ${GCC_TC_VER}")
# Target-side per-arch package root. toolchain4 layout:
#
# ${TCROOT}/gcc-${GCC_TC_VER}/${ARCH}/<pkg>/<ver>/
#
# The "gcc-" prefix leaves room for other compiler families in the future
# (clang/llvm slots) and the dotted version matches the rest of the
# ecosystem (vs. the underscore-mangled path used in toolchain3).
set(TCROOT_ARCH_DIR ${TCROOT}/gcc-${GCC_TC_VER}/${ARCH})
set(TCROOT_ARCH_DIR_PARENT ${TCROOT}/gcc-${GCC_TC_VER})
set(BUSYBOX_DIR ${TCROOT_ARCH_DIR}/busybox/${VER_BUSYBOX})
set(NCURSES_DIR ${TCROOT_ARCH_DIR}/ncurses)
set(ZLIB_DIR ${TCROOT_ARCH_DIR}/zlib/${VER_ZLIB})
set(GNUEFI_DIR ${TCROOT_ARCH_DIR}/gnuefi/${VER_GNUEFI})
set(LIBFDT_DIR ${TCROOT_ARCH_DIR}/dtc/${VER_DTC}/libfdt)
set(LCOV_DIR ${TCROOT}/noarch/lcov/${VER_LCOV})
set(BUSYBOX_CONFIG ${BUSYBOX_DIR}/.config)
# BEGIN [ARCH-dependent paths]
# Musl toolchains live under HOST_DIR_PORTABLE because they are 100%
# statically-linked and libc-independent. toolchain4 layout:
#
# ${HOST_DIR_PORTABLE}/gcc-${ARCH}-musl/${GCC_TC_VER}/
#
# The GCC version is INSIDE the package dir (not embedded in the name),
# so a single gcc-<arch>-musl package can carry multiple versions side
# by side.
set(GCC_TC_ROOT ${HOST_DIR_PORTABLE}/gcc-${ARCH}-musl/${GCC_TC_VER})
set(GCC_TC_ROOT_i386 ${HOST_DIR_PORTABLE}/gcc-i386-musl/${GCC_TC_VER})
set(GCC_TC_ROOT_x86_64 ${HOST_DIR_PORTABLE}/gcc-x86_64-musl/${GCC_TC_VER})
set(GCC_TOOLCHAIN ${GCC_TC_ROOT}/bin)
set(GCC_TOOLCHAIN_i386 ${GCC_TC_ROOT_i386}/bin)
set(GCC_TOOLCHAIN_x86_64 ${GCC_TC_ROOT_x86_64}/bin)
set(GCC_TC_LIBC_DIRNAME ${ARCH_GCC_TC}-linux-musl)
set(GCC_TC_SYSROOT ${GCC_TC_ROOT}/${GCC_TC_LIBC_DIRNAME})
set(GCC_TC_ISYSTEM ${GCC_TC_SYSROOT}/usr/include)
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
#
# The darwin-packaged musl toolchain installs the target's
# system headers directly under <sysroot>/include, not under
# <sysroot>/usr/include. Override GCC_TC_ISYSTEM here so the
# single existing consumer in kernel/arch/CMakeLists.txt
# (which translates to -isystem) resolves the correct path
# on this host. The Linux toolchain keeps the /usr/include
# layout — leave the default in place for it.
#
set(GCC_TC_ISYSTEM ${GCC_TC_SYSROOT}/include)
endif()
# END [ARCH-dependent paths]
if (BOARD)
set(BOARD_BSP ${CMAKE_SOURCE_DIR}/other/bsp/${ARCH}/${BOARD})
include(${BOARD_BSP}/board_bsp.cmake)
endif()
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(msg "SYS C compiler: ${CMAKE_C_COMPILER_ID}")
set(msg "${msg} ${CMAKE_C_COMPILER_VERSION}")
message(STATUS "${msg}")
set(msg "SYS CXX compiler: ${CMAKE_CXX_COMPILER_ID}")
set(msg "${msg} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "${msg}")
# Include the main lists of compiler flags used in the project
include(other/cmake/compiler_flags.cmake)
include(other/cmake/wrapped_syms.cmake)
include(other/cmake/build_modules.cmake)
foreach (opt ${kernel_opts_list})
message(STATUS "${opt} = ${${opt}}")
endforeach()
message(STATUS "****************** KERNEL MODULES *******************")