how to inject a preprocessor #define into the ndk-build

7,151 views
Skip to first unread message

Nalin Savara

unread,
Feb 17, 2013, 2:14:08 AM2/17/13
to andro...@googlegroups.com
Hi Folks...

how to inject a preprocessor #define into the ndk-build.

Do let me know...

Regards,

Nalin
ps:  Apologies if this is a n00b question that gets a RTFM type answer... but a reply I would much appreciate

Chen Yang

unread,
Feb 17, 2013, 2:16:51 AM2/17/13
to andro...@googlegroups.com
add -DXXX to your CFLAGS in your makefile may helo you.
--
Chen



--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nalin Savara

unread,
Feb 17, 2013, 2:20:23 AM2/17/13
to andro...@googlegroups.com
my question below:-

On Sun, Feb 17, 2013 at 12:46 PM, Chen Yang <sunse...@gmail.com> wrote:
add -DXXX to your CFLAGS in your makefile may helo you.
--
Chen

Thanks Chen-- but that I know ; like gcc -D[conditional-define]

However, if we are doing a Android build using the ndk-build script-- does ndk-build script also have a similar command-line option ?

Asking because I have a project around a big SDK-- which has a complicated build script-- which repeatedly calls ndk-build-- am looking to edit that build script-- such that each call to ndk-build includes the conditional defines I want-- possibly picked from that build script's command-line.

Thanks again and Best Regards,

Nalin
 

Ray Donnelly

unread,
Feb 17, 2013, 5:27:19 AM2/17/13
to andro...@googlegroups.com
Hi Nalin,

ndk-build isn't a script so much as just a set of Makefiles. However,
it does require obeying it's own conventions.

Now the RTFM part I'm afraid:

android-ndk/docs/ANDROID-MK.html around line 481:
LOCAL_CFLAGS
~line 505:
LOCAL_CXXFLAGS
~line 509
LOCAL_CPPFLAGS

(I've just spotted a mix-up in the docs between LOCAL_CPPFLAGS and
LOCAL_CXXFLAGS actually!)

I would say that reading docs is a very good idea.

Cheers,

Ray.

Nalin Savara

unread,
Feb 17, 2013, 5:30:54 AM2/17/13
to andro...@googlegroups.com
@Ray, All:
My answer below:

On Sun, Feb 17, 2013 at 3:57 PM, Ray Donnelly <mingw....@gmail.com> wrote:
Hi Nalin,

ndk-build isn't a script so much as just a set of Makefiles. However,
it does require obeying it's own conventions.

Now the RTFM part I'm afraid: 
android-ndk/docs/ANDROID-MK.html around line 481:
LOCAL_CFLAGS
~line 505:

so basically you are saying that ndk-build does'nt take command-line parameters... and we need to edit NDK build itself even if we just need some #defines on a per project basis ?

I have done similar stuff in past--- but have often wondered about the official elegant solution to this common problem...

Just wondering...

Thanks and Regards,

Nalin

Ray Donnelly

unread,
Feb 17, 2013, 5:37:53 AM2/17/13
to andro...@googlegroups.com
No that'd be horrible.

The ndk-build system uses Android.mk from the jni folder of your
Android application's source tree, allowing you to customize things on
a per-project basis.

This file also allows you to specify a whole host of other things such
as multiple libraries to build and link in the same invocation of
ndk-build, it's quite comprehensive, hence RTFM ;-)

Another worthwhile thing to do would be to examine some of the samples.

Cheers,

Ray.

David Turner

unread,
Feb 18, 2013, 5:52:31 AM2/18/13
to andro...@googlegroups.com
On Sun, Feb 17, 2013 at 8:20 AM, Nalin Savara <nsn...@gmail.com> wrote:
my question below:-

On Sun, Feb 17, 2013 at 12:46 PM, Chen Yang <sunse...@gmail.com> wrote:
add -DXXX to your CFLAGS in your makefile may helo you.
--
Chen

Thanks Chen-- but that I know ; like gcc -D[conditional-define]

However, if we are doing a Android build using the ndk-build script-- does ndk-build script also have a similar command-line option ?

Not exactly a command-line option, but since ndk-build is a wrapper around GNU Make, you can force the definition of some variables as in:

ndk-build APP_CFLAGS=-DXXXX

Which will override any definition of APP_CFLAGS you have in your Application.mk.

I would still not recommend doing though because it can lead to incorrect builds when you switch between such definitions. The reason for this is that the build system will not detect that compiler flags changed between invocations, so you will end up linking "stale" object files into the final binaries.

When you manually modify Application.mk instead, the build system detects it and rebuilds everything appropriately.

Yes, some build systems are more advanced than GNU Make and can deal with it, but that's not the case of ndk-build.
A simple work-around is to use the -B flag, which forces a full recompilation. This prevents any wrongdoing, but of course negates the benefit of incremental recompilation.
Another option is to define a custom NDK_OUT value for each such build, so that the object files are never mixed up in a surprising way. 

Asking because I have a project around a big SDK-- which has a complicated build script-- which repeatedly calls ndk-build-- am looking to edit that build script-- such that each call to ndk-build includes the conditional defines I want-- possibly picked from that build script's command-line.

Thanks again and Best Regards,

Nalin
 

--

Nalin Savara

unread,
Feb 18, 2013, 6:00:14 AM2/18/13
to andro...@googlegroups.com
Hi David,
Firstly, thanks a million-- mucho appreciato.

Secondly:
My further questions below quoted text.

On Mon, Feb 18, 2013 at 4:22 PM, David Turner <di...@android.com> wrote:


On Sun, Feb 17, 2013 at 8:20 AM, Nalin Savara <nsn...@gmail.com> wrote:
my question below:-

On Sun, Feb 17, 2013 at 12:46 PM, Chen Yang <sunse...@gmail.com> wrote:
add -DXXX to your CFLAGS in your makefile may helo you.
--
Chen

Thanks Chen-- but that I know ; like gcc -D[conditional-define]

However, if we are doing a Android build using the ndk-build script-- does ndk-build script also have a similar command-line option ?

Not exactly a command-line option, but since ndk-build is a wrapper around GNU Make, you can force the definition of some variables as in:

ndk-build APP_CFLAGS=-DXXXX

Which will override any definition of APP_CFLAGS you have in your Application.mk.


>
>Not exactly a command-line option, but since ndk-build is a wrapper around GNU Make, you 
>can force the >definition of some variables as in:
>
>ndk-build APP_CFLAGS=-DXXXX
>
>Which will override any definition of APP_CFLAGS you have in your Application.mk.
>
 
Does this mean:-
(a) that the variables defined with 
"ndk-build APP_CFLAGS=-DXXXX"
-->> will be defined in addition to variables defined in Application.mk ?
OR
(b) does this mean that only the variables we define on command-line will exist-- and others originally defined using APP_CFLAGS in Application.mk will now be left underined unless defined from command-line ?

(c) if (b)-->>> then is there a option which can define the cmd-line vars in addition to those already defined in Application.mk ?
Something like Qt's libs += libxx ?

Do let me know...

Thanks again for sharing and Best Regards,

Nalin 

I would still not recommend doing though because it can lead to incorrect builds when you switch between such definitions. The reason for this is that the build system will not detect that compiler flags changed between invocations, so you will end up linking "stale" object files into the final binaries.

When you manually modify Application.mk instead, the build system detects it and rebuilds everything appropriately.

<snip> 

David Turner

unread,
Feb 18, 2013, 7:17:24 AM2/18/13
to andro...@googlegroups.com
On Mon, Feb 18, 2013 at 12:00 PM, Nalin Savara <nsn...@gmail.com> wrote:
 
Does this mean:-
(a) that the variables defined with 
"ndk-build APP_CFLAGS=-DXXXX"
-->> will be defined in addition to variables defined in Application.mk ?

It simply means the value will be completely overriden, and any assignment within the ndk-build makefiles will be ignored. It's a GNU Make "feature", see http://www.gnu.org/software/make/manual/html_node/Overriding.html

Note that it can have surprising results if you use it against other variables, but generally speaking the APP_XXX variables are safe. Don't try that with LOCAL_XXX variables, because ndk-build needs to modify their values frequently, and doing so is going to wreak havok.

Again, I insiste this is an "advanced" feature of GNU Make that you should only use if you know exactly what you're doing. I'm guessing this may not the case here, so please take extreme caution.
 
OR
(b) does this mean that only the variables we define on command-line will exist-- and others originally defined using APP_CFLAGS in Application.mk will now be left underined unless defined from command-line ?

Nope, only the one you "define" on the command line are overriden.

(c) if (b)-->>> then is there a option which can define the cmd-line vars in addition to those already defined in Application.mk ?
Something like Qt's libs += libxx ?

Nope :-)
 
Do let me know...

Thanks again for sharing and Best Regards,

Nalin 

I would still not recommend doing though because it can lead to incorrect builds when you switch between such definitions. The reason for this is that the build system will not detect that compiler flags changed between invocations, so you will end up linking "stale" object files into the final binaries.

When you manually modify Application.mk instead, the build system detects it and rebuilds everything appropriately.

<snip> 

--

joebowbeer

unread,
Feb 18, 2013, 12:53:06 PM2/18/13
to andro...@googlegroups.com
You could inject anything you want into your jni src using Ant's replaceregexp or something similar...

You could also use environment variables.

If you are trying to pass build-time parameters using an Ant script, you can pass these as environment variables.

For example:

  <exec executable="ndk-build">
      <arg value="V=1" />
      <!-- used in jni/Android.mk -->
      <env key="BUILD_FOO" value="${build.foo}" />
  </exec>

with corresponding handling in Android.mk:

ifeq ($(BUILD_FOO),bar)
LOCAL_CFLAGS += -DFOOBAR
endif
Reply all
Reply to author
Forward
0 new messages