How to build a bare metal program with meson

1,145 views
Skip to first unread message

Wink Saville

unread,
Oct 12, 2015, 10:13:27 PM10/12/15
to The Meson Build System
I've created a trivial bare metal application that prints "Hi\n" using a Makefile, but would like to use Meson.
My feeble first attempt fails and I'm hoping someone could give me some additional guidance:

wink@wink-envy build]$ meson .. --cross-file ../arm-eabi-cross_file.txt --buildtype plain
The Meson build system
Version: 0.27.0-research
Source dir: /home/wink/prgs/libs/baremetal-hi
Build dir: /home/wink/prgs/libs/baremetal-hi/build
Build type: cross build
Host machine cpu: arm
Target machine cpu: arm
Build machine cpu: x86_64
Project name: Bare metal Hi
/home/wink/opt/cross/lib/gcc/arm-eabi/5.2.0/../../../../arm-eabi/bin/ld: cannot find crt0.o: No such file or directory
/home/wink/opt/cross/lib/gcc/arm-eabi/5.2.0/../../../../arm-eabi/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

Meson encountered an error in file meson.build, line 1, column 0:
Compiler /home/wink/opt/cross/bin/arm-eabi-gcc can not compile programs.


Thanks in advance,

Wink


Jussi Pakkanen

unread,
Oct 14, 2015, 12:15:29 PM10/14/15
to Wink Saville, The Meson Build System
On Tue, Oct 13, 2015 at 5:13 AM, Wink Saville <wi...@saville.com> wrote:
I've created a trivial bare metal application that prints "Hi\n" using a Makefile, but would like to use Meson.
My feeble first attempt fails and I'm hoping someone could give me some additional guidance:

The reason this is happening is that Meson will test all compilers by compiling a small sample program before doing anything else. In this case it fails, probably due to some missing command line arguments or other settings.

Could you compile a simple helloworld application manually and tell what command line works for you? You can find the test source Meson tries to compile in meson-private/sanitycheckc.c, so if you have it handy just use that one.

Wink Saville

unread,
Oct 14, 2015, 10:31:36 PM10/14/15
to The Meson Build System, wi...@saville.com
My bare metal application is about as simple as you can get for a bare metal
application. It consists for a startup.S arm file that does nothing but setup
a stack and invoke "main(void)" in test.c.

The GNU Makefile is pretty simple:

[wink@wink-envy baremetal-hi]$ cat Makefile
# Delete implict rules
.SUFFIXES:

cross=arm-eabi-
cpu=arm926ej-s

CC=$(cross)gcc
LD=$(cross)ld
OC=$(cross)objcopy

cflags=-mcpu=$(cpu) -I. -Wall -O2 -g

.PHONY: all
all: test.bin

test.o: test.c
$(CC) $(cflags) -c -o test.o test.c

startup.o: startup.S
$(CC) $(cflags) -c -o startup.o startup.S

test.bin: test.o startup.o
$(LD) -T link.ld test.o startup.o -o test.elf
$(OC) -O binary test.elf test.bin

.PHONY: run
run: test.bin
qemu-system-arm -M versatilepb -m 128M -nographic -kernel test.bin
  
.PHONY: clean
clean:
rm -rf *.o *.elf *.bin

The sanitycheckcpp.mm file won't compile because it attempts to "#import<stdio.h>"
which doesn't exist for my bare metal application. Also it's a c++ application whereas
I'm only using "c" and my project statement only says I'm using 'c' and 'as':

project('Bare metal Hi', ['c','as'])

So it would be inappropriate to use "c++" for a sanity check and
of course only compilation can be tested as I don't think meson
can always rely on being able to run the resulting sanity check code.

So what do you suggest the next step should be?

Thanks,

Wink

Jussi Pakkanen

unread,
Oct 15, 2015, 12:00:37 PM10/15/15
to Wink Saville, The Meson Build System
On Thu, Oct 15, 2015 at 5:31 AM, Wink Saville <wi...@saville.com> wrote:
 
The sanitycheckcpp.mm file won't compile because it attempts to "#import<stdio.h>"which doesn't exist for my bare metal application. Also it's a c++ application whereas
I'm only using "c" and my project statement only says I'm using 'c' and 'as':

You should not have sanitycheckcpp.mm file in there, that's for objective c++. I assume you ran ./run_tests.sh and just checked out what remained in the work dir.

If this is the case then do run Meson on your own project and then check out meson-private in your build dir. There should be a sanitycheckc.c in there.
 
project('Bare metal Hi', ['c','as'])

So it would be inappropriate to use "c++" for a sanity check and
of course only compilation can be tested as I don't think meson
can always rely on being able to run the resulting sanity check code.

Meson only tries to run the executable if an exe wrapper is defined. The sanity check binary also does not try to use stdio or anything like that. If you can get the C source and test what flags are needed to compile it, I can fix it.

PS Don't put "as" in the list of languages, it will fail. You can put .S files into list of sources, and GCC will do the right thing automatically AFAICR.

Wink Saville

unread,
Oct 15, 2015, 3:05:49 PM10/15/15
to Jussi Pakkanen, The Meson Build System
My mistake I was looking for sanitycheck in the meson sources. I've created pull request #282 which resolves the sanitycheck issue for me.

Wink Saville

unread,
Oct 16, 2015, 2:44:18 AM10/16/15
to The Meson Build System, jpak...@gmail.com
I also needed to fix cross linking and created pull request #283. With
both changes I was successfully able to use meson to create a bare metal
application.
Reply all
Reply to author
Forward
0 new messages