Merge "x86: add syscalls for logging on Android"
diff --git a/Android.mk b/Android.mk
index d932b7e..9295ef1 100644
--- a/Android.mk
+++ b/Android.mk
@@ -36,27 +36,42 @@
 LOCAL_MODULE_CLASS := STATIC_LIBRARIES
 generated_sources_dir := $(local-generated-sources-dir)
 
-$(generated_sources_dir)/$(TARGET_ARCH)/libsyscalls.c: PRIVATE_CUSTOM_TOOL = $< "$(lastword $(CLANG)) -isystem bionic/libc/kernel/uapi/asm-$(TARGET_ARCH)" $@
-$(generated_sources_dir)/$(TARGET_ARCH)/libsyscalls.c: $(LOCAL_PATH)/gen_syscalls.sh
+my_gen := $(generated_sources_dir)/$(TARGET_ARCH)/libsyscalls.c
+# We need the quotes so the shell script treat them as one argument.
+my_cc := "$(lastword $(CLANG)) \
+    $(addprefix -isystem ,$(TARGET_C_INCLUDES)) \
+    $(CLANG_TARGET_GLOBAL_CFLAGS)"
+$(my_gen): PRIVATE_CC := $(my_cc)
+$(my_gen): PRIVATE_CUSTOM_TOOL = $< $(PRIVATE_CC) $@
+$(my_gen): $(LOCAL_PATH)/gen_syscalls.sh
 	$(transform-generated-source)
-LOCAL_GENERATED_SOURCES_$(TARGET_ARCH) += $(generated_sources_dir)/$(TARGET_ARCH)/libsyscalls.c
+LOCAL_GENERATED_SOURCES_$(TARGET_ARCH) += $(my_gen)
 
-$(generated_sources_dir)/$(TARGET_ARCH)/libconstants.c: PRIVATE_CUSTOM_TOOL = $< "$(lastword $(CLANG)) -isystem bionic/libc/kernel/uapi/asm-$(TARGET_ARCH)" $@
-$(generated_sources_dir)/$(TARGET_ARCH)/libconstants.c: $(LOCAL_PATH)/gen_constants.sh
+my_gen := $(generated_sources_dir)/$(TARGET_ARCH)/libconstants.c
+$(my_gen): PRIVATE_CC := $(my_cc)
+$(my_gen): PRIVATE_CUSTOM_TOOL = $< $(PRIVATE_CC) $@
+$(my_gen): $(LOCAL_PATH)/gen_constants.sh
 	$(transform-generated-source)
-LOCAL_GENERATED_SOURCES_$(TARGET_ARCH) += $(generated_sources_dir)/$(TARGET_ARCH)/libconstants.c
+LOCAL_GENERATED_SOURCES_$(TARGET_ARCH) += $(my_gen)
 
 # For processes running in 32-bit compat mode on 64-bit processors.
 ifdef TARGET_2ND_ARCH
-$(generated_sources_dir)/$(TARGET_2ND_ARCH)/libsyscalls.c: PRIVATE_CUSTOM_TOOL = $< "$(lastword $(CLANG)) -isystem bionic/libc/kernel/uapi/asm-$(TARGET_2ND_ARCH)" $@
-$(generated_sources_dir)/$(TARGET_2ND_ARCH)/libsyscalls.c: $(LOCAL_PATH)/gen_syscalls.sh
+my_gen := $(generated_sources_dir)/$(TARGET_2ND_ARCH)/libsyscalls.c
+my_cc := "$(lastword $(CLANG)) \
+    $(addprefix -isystem ,$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_C_INCLUDES)) \
+    $($(TARGET_2ND_ARCH_VAR_PREFIX)CLANG_TARGET_GLOBAL_CFLAGS)"
+$(my_gen): PRIVATE_CC := $(my_cc)
+$(my_gen): PRIVATE_CUSTOM_TOOL = $< $(PRIVATE_CC) $@
+$(my_gen): $(LOCAL_PATH)/gen_syscalls.sh
 	$(transform-generated-source)
-LOCAL_GENERATED_SOURCES_$(TARGET_2ND_ARCH) += $(generated_sources_dir)/$(TARGET_2ND_ARCH)/libsyscalls.c
+LOCAL_GENERATED_SOURCES_$(TARGET_2ND_ARCH) += $(my_gen)
 
-$(generated_sources_dir)/$(TARGET_2ND_ARCH)/libconstants.c: PRIVATE_CUSTOM_TOOL = $< "$(lastword $(CLANG)) -isystem bionic/libc/kernel/uapi/asm-$(TARGET_2ND_ARCH)" $@
-$(generated_sources_dir)/$(TARGET_2ND_ARCH)/libconstants.c: $(LOCAL_PATH)/gen_constants.sh
+my_gen := $(generated_sources_dir)/$(TARGET_2ND_ARCH)/libconstants.c
+$(my_gen): PRIVATE_CC := $(my_cc)
+$(my_gen): PRIVATE_CUSTOM_TOOL = $< $(PRIVATE_CC) $@
+$(my_gen): $(LOCAL_PATH)/gen_constants.sh
 	$(transform-generated-source)
-LOCAL_GENERATED_SOURCES_$(TARGET_2ND_ARCH) += $(generated_sources_dir)/$(TARGET_2ND_ARCH)/libconstants.c
+LOCAL_GENERATED_SOURCES_$(TARGET_2ND_ARCH) += $(my_gen)
 endif
 
 LOCAL_CFLAGS := $(minijailCommonCFlags)
diff --git a/gen_constants.sh b/gen_constants.sh
index 7b39156..9f217ee 100755
--- a/gen_constants.sh
+++ b/gen_constants.sh
@@ -40,7 +40,7 @@
 #   { "AT_FDWCD", AT_FDCWD },
 # endif
 SED_MULTILINE='s@#define ([[:upper:]][[:upper:]0-9_]*).*@#ifdef \1\
-  { "\1", \1 },\
+  { "\1", (unsigned long) \1 },\
 #endif  // \1@'
 
 # Passes the previous list of #includes to the C preprocessor and prints out
@@ -54,7 +54,7 @@
 const struct constant_entry constant_table[] = {
 $(echo "$INCLUDES" | \
   ${CC} -dD - -E | \
-  grep '^#define [[:upper:]][[:upper:]0-9_]* ' | \
+  grep -E '^#define [[:upper:]][[:upper:]0-9_]*(\s)+[[:alnum:]]' | \
   grep -Ev '(SIGRTMAX|SIGRTMIN|SIG_|NULL)' | \
   sort -u | \
   sed -Ee "${SED_MULTILINE}")