fix deciding which externals directories to build

The way it was doing it before was completely brain-dead.

Change-Id: Id1baac54b92f0cb4e68550fa135398bea02b060f
diff --git a/aos/build/aos.gypi b/aos/build/aos.gypi
index c390052..f7c5f07 100644
--- a/aos/build/aos.gypi
+++ b/aos/build/aos.gypi
@@ -105,7 +105,7 @@
           ],
         },
       },
-    ], ['SANITIZER_FPIE!=""', {
+    ], ['EXTERNALS_EXTRA=="-fPIE"', {
         'target_defaults': {
           'cflags': [
             '-fPIE',
diff --git a/aos/build/build.py b/aos/build/build.py
index f0300f9..6dea832 100755
--- a/aos/build/build.py
+++ b/aos/build/build.py
@@ -611,13 +611,16 @@
   def download_externals(self, platforms):
     to_download = set()
     for architecture in PrimeProcessor.ARCHITECTURES:
+      pie_sanitizers = set()
       for sanitizer in PrimeProcessor.PIE_SANITIZERS:
-        if platforms & self.select_platforms(architecture=architecture,
-                                             sanitizer=sanitizer):
-          to_download.add(architecture + '-fPIE')
-        if platforms & self.select_platforms(architecture=architecture,
-                                             sanitizer='none'):
-          to_download.add(architecture)
+        pie_sanitizers.update(self.select_platforms(architecture=architecture,
+                                                    sanitizer=sanitizer))
+      if platforms & pie_sanitizers:
+        to_download.add(architecture + '-fPIE')
+
+      if platforms & (self.platforms() - pie_sanitizers):
+        to_download.add(architecture)
+
     for download_target in to_download:
       call_download_externals(download_target)
 
@@ -955,7 +958,7 @@
              '-DFULL_COMPILER=%s' % platform.compiler(),
              '-DDEBUG=%s' % ('yes' if platform.debug() else 'no'),
              '-DSANITIZER=%s' % platform.sanitizer(),
-             '-DSANITIZER_FPIE=%s' %
+             '-DEXTERNALS_EXTRA=%s' %
              ('-fPIE' if platform.sanitizer() in PrimeProcessor.PIE_SANITIZERS
               else '')) +
             processor.extra_gyp_flags() + (args.main_gyp,),
diff --git a/aos/build/externals.gyp b/aos/build/externals.gyp
index 44ca1d7..a097fd7 100644
--- a/aos/build/externals.gyp
+++ b/aos/build/externals.gyp
@@ -4,8 +4,8 @@
   'variables': {
     'externals': '<(AOS)/../output/downloaded',
     'externals_abs': '<!(readlink -f ../../output/downloaded)',
-    'compiled': '<(externals)/../compiled-<(ARCHITECTURE)<(SANITIZER_FPIE)',
-    'compiled_abs': '<(externals_abs)/../compiled-<(ARCHITECTURE)<(SANITIZER_FPIE)',
+    'compiled': '<(externals)/../compiled-<(ARCHITECTURE)<(EXTERNALS_EXTRA)',
+    'compiled_abs': '<(externals_abs)/../compiled-<(ARCHITECTURE)<(EXTERNALS_EXTRA)',
 
 # These versions have to be kept in sync with the ones in download_externals.sh.
     'eigen_version': '3.2.1',