Skip to content

Commit b32a7ab

Browse files
authored
Clean up Cross-Compile and Instructions (#301)
* - Update makefiles to simplify selection of cross-compile. - Separate USER and KERNEL LDFLAGS and CCFLAGS. - Update cross-compilation instructions accordingly. * Cross compile false by default.
1 parent 6426849 commit b32a7ab

4 files changed

Lines changed: 53 additions & 29 deletions

File tree

Makefile.config

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1-
KERNEL_CCFLAGS=-Wall -c -ffreestanding -fno-pie -g -std=gnu99
1+
# By default, attempt to compile using the host compiler toolchain.
2+
# If you have built the cross-compiler, then set this to true instead.
3+
CROSS_COMPILE=false
24

3-
# These settings select the native compiler,
4-
# which is likely to work on native linux-x86.
5-
#
6-
CC=gcc -m32
7-
LD=ld
8-
KERNEL_LD=ld -melf_i386
9-
AR=ar
10-
OBJCOPY=objcopy
11-
ISOGEN=genisoimage
5+
# This roundabout method finds the directory containing this Makefile.config.
6+
THISFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
7+
THISFILE_DIR := $(dir $(THISFILE_PATH))
128

13-
# If you are compiling from another platform,
14-
# then use the script build-cross-compiler.sh
15-
# add cross/bin to your path, and uncomment these lines:
16-
#CC=i686-elf-gcc
17-
#LD=i686-elf-ld
18-
#KERNEL_LD=i686-elf-ld
19-
#AR=i686-elf-ar
20-
#OBJCOPY=i686-elf-objcopy
9+
ifeq ($(CROSS_COMPILE),true)
10+
# Select the cross-compilation tools.
11+
CROSS := $(THISFILE_DIR)/cross
12+
CC=$(CROSS)/bin/i686-elf-gcc
13+
LD=$(CROSS)/bin/i686-elf-ld
14+
KERNEL_LD=$(CROSS)/bin/i686-elf-ld
15+
AR=$(CROSS)/bin/i686-elf-ar
16+
OBJCOPY=$(CROSS)/bin/i686-elf-objcopy
17+
else
18+
# Select the host tools instead.
19+
CC=gcc -m32
20+
LD=ld
21+
KERNEL_LD=ld -melf_i386
22+
AR=ar
23+
OBJCOPY=objcopy
24+
endif
25+
26+
# Compiler and linker settings for the kernel proper:
27+
KERNEL_CCFLAGS=-Wall -c -ffreestanding -fno-pie -g -std=gnu99
28+
KERNEL_LDFLAGS=
2129

30+
# Compiler and linker settings for the library and user code:
31+
USER_CCFLAGS=-Wall -c -ffreestanding -fno-pie -g -std=gnu99
32+
USER_LDFLAGS=-z max-page-size=4096 -T basekernel.user.ldscript
33+
34+
# Linux machines typically install genisoimage for cdrom imaging.
35+
ISOGEN=genisoimage
2236
# If building on OSX, then install mkisofs via ports or brew
2337
# and uncomment this:
2438
#ISOGEN=mkisofs
39+

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ To learn more, see the [Basekernel Wiki](https://github.com/dthain/basekernel/wi
2525
## Quick Start Instructions
2626

2727
If you are building on a Linux-X86 machine
28-
and have the QEMU virtual machine installed:
28+
and have the QEMU virtual machine installed,
29+
then it could be as easy as this to build and run:
2930

3031
```
3132
git clone https://github.com/dthain/basekernel
@@ -34,7 +35,7 @@ make
3435
qemu-system-i386 -cdrom basekernel.iso
3536
```
3637

37-
And you should see something like this:
38+
You should see something like this:
3839

3940
<img src=screenshot.png align=center>
4041

@@ -68,11 +69,19 @@ run /bin/manager.exe
6869
Press TAB to change the focus between windows,
6970
and you can interact with each process in turn.
7071

71-
## Cross-Compiling Instructions
72+
## Not So Quick Start Instructions
7273

73-
If you are building on any other type of machine,
74-
you will probably need to build a cross-compiler
75-
using `build-cross-compiler.sh` and then edit
76-
`Makefile.config` to use the cross compiler binaries,
77-
then execute `make` to create `basekernel.iso`
74+
If you are building on any other type of machine (not Linux or not-X86)
75+
then you will need to build a cross-compiler toolchain first:
7876

77+
1 - Run `./build-cross-compiler.sh` which will download and build the necessary compiler, linker,
78+
debugger, etc to create and run 32-bit X86 code. **Be patient: this could take an hour or longer to complete.**
79+
80+
2 - Double-check that the cross-compiler was built correctly:
81+
```
82+
i686-elf-gcc --version
83+
```
84+
85+
3 - Modify `Makefile.config` and set `CROSS_COMPILE=true`
86+
87+
4 - Build with `make` and then proceed as normal.

library/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LIBRARY_OBJECTS=errno.o syscall.o syscalls.o string.o stdio.o stdlib.o malloc.o
55
all: user-start.o baselib.a
66

77
%.o: %.c
8-
${CC} ${KERNEL_CCFLAGS} -I ../include $< -o $@
8+
${CC} ${USER_CCFLAGS} -I ../include $< -o $@
99

1010
baselib.a: ${LIBRARY_OBJECTS}
1111
${AR} rv $@ ${LIBRARY_OBJECTS}

user/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ USER_PROGRAMS=ball.exe clock.exe copy.exe livestat.exe manager.exe fractal.exe p
66
all: $(USER_PROGRAMS)
77

88
%.o: %.c
9-
${CC} ${KERNEL_CCFLAGS} -I ../include $< -o $@
9+
${CC} ${USER_CCFLAGS} -I ../include $< -o $@
1010

1111
%.exe: %.o ../library/user-start.o ../library/baselib.a
12-
${LD} -z max-page-size=4096 -T basekernel.user.ldscript $< ../library/baselib.a -o $@ -Map $@.map
12+
${LD} ${USER_LDFLAGS} $< ../library/baselib.a -o $@
1313

1414
clean:
1515
rm -rf *.exe *.o

0 commit comments

Comments
 (0)