1

Issue: I have 2 macros I added in a c file and I have 2 machines. Using Yocto recipe I wish to build the code with specific macros for specific machines.

Breaking down the issue in sub parts - hello.c

// Simple hello.c program
#include<stdio.h>

void main(){
    printf("Hello World!\n");

    //Introducing Macro 1 -- abc

    #ifdef abc
    printf("Day 1\n");
    #endif

    //Introducing Macro 2 -- xyz

    #ifdef xyz
    printf("Day 2\n");
    #endif
}

Expected & Observed Output:

gcc hello.c -o hello
./hello
Hello World!

gcc hello.c -Dabc -o test1
./test1
Hello World!
Day 1 

gcc hello.c -Dxyz -o test2
./test2
Hello World!
Day 2

Makefile Contents:

obj-m := hello.o

SRC := ${shell pwd}

CFLAGS += -Dabc

all:
    ${MAKE} -C M=${SRC} ${CFLAGS}

test_install:
    ${MAKE} -C M=${SRC} ${CFLAGS} test_install

clean:
    rm -rf *.o

Have checked the above Makefile contents using cat -e -t Makefile. It is properly terminated with $ at end of each line. Since I am using MAKE, it does have the gcc compiler. In the Makefile I have hardcoded CFLAGS="-Dabc" , intention being if the code is compiled accordingly then I can add ifeq condition to the Makefile handle the other CFLAGS="-Dxyz"

Recipe File Contents:

S = "${WORKDIR}/helloworld/hello"

COMPATIBLE_MACHINE = "oldmachine|newmachine"
EXTRA_OEMAKE_oldmachine += "CFLAGS=-Dabc"
EXTRA_OEMAKE_newmachine += "CFLAGS=-Dxyz"

do_compile(){
    ${EXTRA_OEMAKE}_${MACHINE}
}

The default function of do_compile is to run oe_runmake, I assumed that it will be picking up this CFLAGS which I am trying to pass.

I checked the contents of the log.do_compile under tmp/work/... folder and I do not see the CFLAGS being used.

The above recipe I also re-wrote as

do_compile_append_oldmachine(){
     CFLAGS="-Dabc"
}

do_compile_append_newmachine(){
     CFLAGS="-Dxyz" #Also tried passing EXTRA_OEMAKE_MACHINE as above
}

This method also, unable to find the CFLAGS part when I check the log.do_compile file under tmp/work/.. directory.

How do I make use the CFLAGS so that it compiles the code as per the specific architecture?

Misc Details:

Under the specific meta layer, I have the directory like this.

helloworld

  • hello.c
  • hello.o
  • Makefile

Note: Due to less rep, could not add CFLAGS as a tag for this.

Philippos
  • 13,237
  • 2
  • 37
  • 76
Nathan
  • 21
  • 4
  • 1
    If you write your own `do_compile`, doesn't it overwrite the default? So you need to actually call `oe_runmake`, not just `${EXTRA_OEMAKE}_${MACHINE}`. I wonder, why this didn't produce an error, but I'm not experienced with yocto. – Philippos Mar 27 '23 at 06:26

0 Answers0