11param (
2- [ValidateSet (" all" , " clean" )]
2+ [ValidateSet (" all" , " clean" , " iso " )]
33 [string ]$Target = " all" ,
44 [string ]$ModelBlob
55)
@@ -12,6 +12,15 @@ function Require-Command([string]$Name) {
1212 }
1313}
1414
15+ function Find-FirstCommand ([string []]$Names ) {
16+ foreach ($name in $Names ) {
17+ if (Get-Command $name - ErrorAction SilentlyContinue) {
18+ return $name
19+ }
20+ }
21+ throw (" None of the following commands were found in PATH: " + ($Names -join " , " ))
22+ }
23+
1524function Invoke-Checked ([string ]$Exe , [string []]$Args ) {
1625 & $Exe @Args
1726 if ($LASTEXITCODE -ne 0 ) {
@@ -41,6 +50,17 @@ $kernelBin = Join-Path $root "kernel.bin"
4150$ldScript = Join-Path $root " linker.ld"
4251$assetsDir = Join-Path $root " assets"
4352
53+ $modelBlobPath = $ModelBlob
54+ if ([string ]::IsNullOrEmpty($modelBlobPath )) {
55+ $candidateA = Join-Path $assetsDir " smollm-135m.gguf"
56+ $candidateB = Join-Path $assetsDir " SmolLM2-135M-Instruct-Q4_K_M.gguf"
57+ if (Test-Path $candidateA ) {
58+ $modelBlobPath = $candidateA
59+ } elseif (Test-Path $candidateB ) {
60+ $modelBlobPath = $candidateB
61+ }
62+ }
63+
4464if ($Target -eq " clean" ) {
4565 if (Test-Path $buildDir ) {
4666 Remove-Item - Recurse - Force $buildDir
@@ -51,9 +71,9 @@ if ($Target -eq "clean") {
5171 exit 0
5272}
5373
54- Require- Command " i686-elf-gcc"
74+ $cc = Find-FirstCommand @ (" i686-elf-gcc" , " i686-linux-gnu-gcc" )
75+ $objcopy = Find-FirstCommand @ (" i686-elf-objcopy" , " i686-linux-gnu-objcopy" )
5576Require- Command " nasm"
56- Require- Command " i686-elf-objcopy"
5777
5878if (-not (Test-Path $ldScript )) {
5979 throw " linker.ld not found"
@@ -69,7 +89,10 @@ $cSrcs = Get-ChildItem -Path $srcDir -Recurse -Filter "*.c"
6989$objPaths = @ ()
7090
7191$asFlags = @ (" -f" , " elf32" )
72- $cFlags = @ (" -ffreestanding" , " -O2" , " -Wall" , " -Wextra" , " -m32" , " -fno-pie" , " -fno-stack-protector" , " -nostdlib" , " -nostdinc" , " -Iinclude" )
92+ $cFlags = @ (" -ffreestanding" , " -O2" , " -Wall" , " -Wextra" , " -m32" , " -fno-pie" , " -fno-PIC" , " -fno-stack-protector" , " -nostdlib" , " -nostdinc" , " -msse" , " -msse2" , " -Iinclude" )
93+ if ([string ]::IsNullOrEmpty($modelBlobPath )) {
94+ $cFlags += " -DNO_AI_MODEL"
95+ }
7396
7497foreach ($src in $asmSrcs ) {
7598 $obj = Get-ObjPath $src.FullName $srcDir $buildDir
@@ -87,27 +110,16 @@ foreach ($src in $cSrcs) {
87110 if (-not (Test-Path $objDir )) {
88111 New-Item - ItemType Directory - Force - Path $objDir | Out-Null
89112 }
90- Invoke-Checked " i686-elf-gcc " ($cFlags + @ (" -c" , $src.FullName , " -o" , $obj ))
113+ Invoke-Checked $cc ($cFlags + @ (" -c" , $src.FullName , " -o" , $obj ))
91114 $objPaths += $obj
92115}
93116
94- $modelBlobPath = $ModelBlob
95- if ([string ]::IsNullOrEmpty($modelBlobPath )) {
96- $candidateA = Join-Path $assetsDir " smollm-135m.gguf"
97- $candidateB = Join-Path $assetsDir " SmolLM2-135M-Instruct-Q4_K_M.gguf"
98- if (Test-Path $candidateA ) {
99- $modelBlobPath = $candidateA
100- } elseif (Test-Path $candidateB ) {
101- $modelBlobPath = $candidateB
102- }
103- }
104-
105117if (-not [string ]::IsNullOrEmpty($modelBlobPath )) {
106118 if (-not (Test-Path $modelBlobPath )) {
107119 throw " Model blob not found: $modelBlobPath "
108120 }
109121 $modelObj = Join-Path $buildDir " model.o"
110- Invoke-Checked " i686-elf- objcopy" @ (
122+ Invoke-Checked $ objcopy @ (
111123 " -I" , " binary" ,
112124 " -O" , " elf32-i386" ,
113125 " -B" , " i386" ,
@@ -118,5 +130,38 @@ if (-not [string]::IsNullOrEmpty($modelBlobPath)) {
118130 $objPaths += $modelObj
119131}
120132
121- $ldFlags = @ (" -T" , $ldScript , " -ffreestanding" , " -O2" , " -nostdlib" , " -Wl,--build-id=none" )
122- Invoke-Checked " i686-elf-gcc" ($ldFlags + @ (" -o" , $kernelBin ) + $objPaths )
133+ $ldFlags = @ (" -T" , $ldScript , " -ffreestanding" , " -O2" , " -nostdlib" , " -m32" , " -no-pie" , " -Wl,-z,noexecstack" , " -Wl,--build-id=none" )
134+ Invoke-Checked $cc ($ldFlags + @ (" -o" , $kernelBin ) + $objPaths )
135+
136+ if ($Target -eq " iso" ) {
137+ $isoRoot = Join-Path $buildDir " isodir"
138+ $isoBoot = Join-Path $isoRoot " boot"
139+ $isoGrub = Join-Path $isoBoot " grub"
140+ $grubCfg = Join-Path $isoGrub " grub.cfg"
141+ $isoPath = Join-Path $buildDir " basicallylinux.iso"
142+
143+ Require- Command " grub-mkrescue"
144+ Require- Command " xorriso"
145+
146+ New-Item - ItemType Directory - Force - Path $isoGrub | Out-Null
147+ Copy-Item - Force $kernelBin (Join-Path $isoBoot " kernel.bin" )
148+ if (-not [string ]::IsNullOrEmpty($modelBlobPath )) {
149+ Copy-Item - Force $modelBlobPath (Join-Path $isoBoot ([System.IO.Path ]::GetFileName($modelBlobPath )))
150+ }
151+
152+ $lines = @ (
153+ " set timeout=5" ,
154+ " set default=0" ,
155+ " menuentry `" BasicallyLinux`" {" ,
156+ " multiboot /boot/kernel.bin"
157+ )
158+ if (-not [string ]::IsNullOrEmpty($modelBlobPath )) {
159+ $modelName = [System.IO.Path ]::GetFileName($modelBlobPath )
160+ $lines += " module /boot/$modelName `" ai_model`" "
161+ }
162+ $lines += " boot"
163+ $lines += " }"
164+ Set-Content - Path $grubCfg - Value $lines - Encoding ASCII
165+
166+ Invoke-Checked " grub-mkrescue" @ (" -o" , $isoPath , $isoRoot )
167+ }
0 commit comments