2

tensorflow/stream_executor/platform/platform.h defines some macros. How does TF set macros like ANDROID, __ANDROID__, PLATFORM_GOOGLE, RASPBERRY_PI?

I think bazel BUILD file may preset some macros like cmake (Define preprocessor macro through cmake) but I cannot find any clue.

skytree
  • 1,060
  • 2
  • 13
  • 38

1 Answers1

1

Take a look in the .bazelrc file. It defines for instance build:android_arm --config=android. This means you can build in bazel with bazel build --config=android - I guess that a compiler with target android provides in the system header an ANDROID and __ANDROID__ define - so you have not to explicitly modify a cc_binary target.

You can also add defines explicitly to a cc_binary:

The cc_binary target provides a define and copts attribute:

cc_binary(name, deps, srcs, data, args, compatible_with, copts, defines, deprecation, distribs, exec_compatible_with, features, includes, licenses, linkopts, linkshared, linkstatic, malloc, nocopts, output_licenses, restricted_to, stamp, tags, testonly, toolchains, visibility, win_def_file)

From the documentation:

defines: List of strings; optional

List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D (or /D on Windows) and added to COPTS. Unlike copts, these flags are added for the target and every rule that depends on it! Be very careful, since this may have far-reaching effects. When in doubt, add "-D" (or /D on Windows) flags to copts instead.

I would recommend you to add your define to the copts attribute, e.g.:

cc_binary(
    srcs = ["main.cpp"],
    copts =  ["-DMY_DEFINE"],
)
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
  • Thank you. So, I think you are saying those macros are set at runtime when user inputs `bazel build` command? Or, if I want to "turn on" those macros, I add `-D` options in BUILD? I just re-confirm my guess about tf's usage. – skytree Jun 06 '19 at 02:17
  • @skytree I modified my answer – Vertexwahn Jun 06 '19 at 08:17