Cross-compiler for C project, commandLine vs Gradle

Hi there,

I am trying to cross-compile a project using Gradle (for a specific processor), and I am having some issues. When I cross-compile my project using a command line, everything works fine. My first task:

task CheckCmd(type:Exec) {
  commandLine '56400-gcc', '--version'
}

gives the following result:

56400-elf-gcc (GCC) 4.9.4
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

My second task

task CompileCmd(type:Exec) {
  commandLine '56400-gcc', '-x', 'c', '-c', '-I/home/nicolas06/experiment/Gradle/MyProject/src/main/headers',   '-Wall', '-Werror',  '-mos=nodeos',  '/home/nicolas06/experiment/Gradle/MyProject/src/main/c/cluster_main.c'
}

compiles the C code without warning or error.

Now, when I try to do the same automatically with Gradle, it does not work.

allprojects {

 apply plugin : 'c'

 model {
     toolChains {
     arch56400_toolchain(Gcc) {
             target("arch56400"){
               cCompiler.executable '56400-gcc'
               cCompiler.withArguments { List<String> args ->
                   args << '-Wall'
                   args << '-Werror'
                   args << '-mos=nodeos'
               }
               assembler.executable '56400-gcc'
               linker.executable '56400-gcc'
             }
         }
     }

     platforms {
         arch56400 {
             architecture "arch56400"
         }
     }

     components {
         main(NativeExecutableSpec) {
          targetPlatform "arch56400"
         }
     }
  }

Executing gradle mainExecutable gives the following result:

$ gradle mainExecutable
> Task :compileMainExecutableMainC FAILED
/home/nicolas06/Documents/Gradle/kalray_again/src/main/c/cluster_main.c: In function ‘main’:
/home/nicolas06/Documents/Gradle/kalray_again/src/main/c/cluster_main.c:35:4: error: implicit declaration of function ‘pthread_attr_setaffinity_np’ [-Werror=implicit-function-declaration]

It’s a compilation error that I get with gradle mainExecutable but not with the commandLine. To be more specific, the flag -mos=nodeos that is specific to the cross-compiler 56400-gcc is not processed correctly and it generates a compilation issue. If I remove that flag from my CompileCmd task, I get the same compilation error.

Nota. The error is created by th following code code in a header (thread.h):

#if defined(__nodeos__)
int     _EXFUN(pthread_attr_setaffinity_np,
        (pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset));

#endif

The if defined(__nodeos__) is true when running the commandLine but not when running the mainExecutable. So, It seems to me that the flag args << '-mos=nodeos' is deleted? removed? set aside? by Gradle when I run a cross-compilation and I don’t understand why. Can anyone help me solve that problem?

Thanks!

2 Likes

A quick update on my own post.

According to the doc I got from our provider, the flag -mos=nodeos sets the macro #define __nodeos__ 1. Thereby, running 56400-gcc -mos=nodeos tells GCC to select Node and not Linux (default) as the operating system layer to use.

I haven’t figured why setting the macro fails with Gradle while it works fine when running the command line on a console but this may help someone from Gradle or from the community to help me solve the problem.

Edit.
For information, option.txt shows that the flag -mos=nodeos is parsed by Gradle… yet the compilation failed as if it was not parsed.

The answer to my own post:

When compiling, Gradle adds all the flags in a file and run the command gcc @file --other_options. The resulting behavior and compilation process are not necessarily similar to gcc -flag1 -flags2 -flag3 --other_options and, in my case, it created compilation errors because some flags were not processed correctly.

Hope it will help other engineers that may face the same issue.