12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- # pylint: disable=no-member, no-self-use, unused-argument
15
+ # pylint: disable=no-member, no-self-use, unused-argument, too-many-lines
16
16
# pylint: disable=too-many-instance-attributes, too-many-public-methods
17
17
18
18
from __future__ import absolute_import
@@ -70,6 +70,8 @@ def get_used_frameworks(env, path):
70
70
71
71
# check source files
72
72
for root , _ , files in os .walk (path , followlinks = True ):
73
+ if "mbed_lib.json" in files :
74
+ return ["mbed" ]
73
75
for fname in files :
74
76
if not env .IsFileWithExt (
75
77
fname , piotool .SRC_BUILD_EXT + piotool .SRC_HEADER_EXT ):
@@ -175,10 +177,11 @@ def src_dir(self):
175
177
if isdir (join (self .path , "src" )) else self .path )
176
178
177
179
def get_include_dirs (self ):
178
- items = [self . src_dir ]
180
+ items = []
179
181
include_dir = self .include_dir
180
182
if include_dir and include_dir not in items :
181
183
items .append (include_dir )
184
+ items .append (self .src_dir )
182
185
return items
183
186
184
187
@property
@@ -260,7 +263,6 @@ def load_manifest(self):
260
263
261
264
def process_extra_options (self ):
262
265
with util .cd (self .path ):
263
- self .env .ProcessUnFlags (self .build_unflags )
264
266
self .env .ProcessFlags (self .build_flags )
265
267
if self .extra_script :
266
268
self .env .SConscriptChdir (1 )
@@ -270,6 +272,7 @@ def process_extra_options(self):
270
272
"env" : self .env ,
271
273
"pio_lib_builder" : self
272
274
})
275
+ self .env .ProcessUnFlags (self .build_unflags )
273
276
274
277
def process_dependencies (self ):
275
278
if not self .dependencies :
@@ -588,6 +591,111 @@ def get_include_dirs(self):
588
591
def is_frameworks_compatible (self , frameworks ):
589
592
return util .items_in_list (frameworks , ["mbed" ])
590
593
594
+ def process_extra_options (self ):
595
+ self ._process_mbed_lib_confs ()
596
+ return super (MbedLibBuilder , self ).process_extra_options ()
597
+
598
+ def _process_mbed_lib_confs (self ):
599
+ mbed_lib_paths = [
600
+ join (root , "mbed_lib.json" )
601
+ for root , _ , files in os .walk (self .path )
602
+ if "mbed_lib.json" in files
603
+ ]
604
+ if not mbed_lib_paths :
605
+ return None
606
+
607
+ mbed_config_path = None
608
+ for p in self .env .get ("CPPPATH" ):
609
+ mbed_config_path = join (self .env .subst (p ), "mbed_config.h" )
610
+ if isfile (mbed_config_path ):
611
+ break
612
+ else :
613
+ mbed_config_path = None
614
+ if not mbed_config_path :
615
+ return None
616
+
617
+ macros = {}
618
+ for mbed_lib_path in mbed_lib_paths :
619
+ macros .update (self ._mbed_lib_conf_parse_macros (mbed_lib_path ))
620
+
621
+ self ._mbed_conf_append_macros (mbed_config_path , macros )
622
+ return True
623
+
624
+ @staticmethod
625
+ def _mbed_normalize_macro (macro ):
626
+ name = macro
627
+ value = None
628
+ if "=" in macro :
629
+ name , value = macro .split ("=" , 1 )
630
+ return dict (name = name , value = value )
631
+
632
+ def _mbed_lib_conf_parse_macros (self , mbed_lib_path ):
633
+ macros = {}
634
+ cppdefines = str (self .env .Flatten (self .env .subst ("$CPPDEFINES" )))
635
+ manifest = util .load_json (mbed_lib_path )
636
+
637
+ # default macros
638
+ for macro in manifest .get ("macros" , []):
639
+ macro = self ._mbed_normalize_macro (macro )
640
+ macros [macro ['name' ]] = macro
641
+
642
+ # configuration items
643
+ for key , options in manifest .get ("config" , {}).items ():
644
+ if "value" not in options :
645
+ continue
646
+ macros [key ] = dict (
647
+ name = options .get ("macro_name" ), value = options .get ("value" ))
648
+
649
+ # overrode items per target
650
+ for target , options in manifest .get ("target_overrides" , {}).items ():
651
+ if target != "*" and "TARGET_" + target not in cppdefines :
652
+ continue
653
+ for macro in options .get ("target.macros_add" , []):
654
+ macro = self ._mbed_normalize_macro (macro )
655
+ macros [macro ['name' ]] = macro
656
+ for key , value in options .items ():
657
+ if not key .startswith ("target." ) and key in macros :
658
+ macros [key ]['value' ] = value
659
+
660
+ # normalize macro names
661
+ for key , macro in macros .items ():
662
+ if not macro ['name' ]:
663
+ macro ['name' ] = key
664
+ if "." not in macro ['name' ]:
665
+ macro ['name' ] = "%s.%s" % (manifest .get ("name" ),
666
+ macro ['name' ])
667
+ macro ['name' ] = re .sub (
668
+ r"[^a-z\d]+" , "_" , macro ['name' ], flags = re .I ).upper ()
669
+ macro ['name' ] = "MBED_CONF_" + macro ['name' ]
670
+ if isinstance (macro ['value' ], bool ):
671
+ macro ['value' ] = 1 if macro ['value' ] else 0
672
+
673
+ return {macro ["name" ]: macro ["value" ] for macro in macros .values ()}
674
+
675
+ def _mbed_conf_append_macros (self , mbed_config_path , macros ):
676
+ lines = []
677
+ with open (mbed_config_path ) as fp :
678
+ for line in fp .readlines ():
679
+ line = line .strip ()
680
+ if line == "#endif" :
681
+ lines .append (
682
+ "// PlatformIO Library Dependency Finder (LDF)" )
683
+ lines .extend ([
684
+ "#define %s %s" % (name ,
685
+ value if value is not None else "" )
686
+ for name , value in macros .items ()
687
+ ])
688
+ lines .append ("" )
689
+ if not line .startswith ("#define" ):
690
+ lines .append (line )
691
+ continue
692
+ tokens = line .split ()
693
+ if len (tokens ) < 2 or tokens [1 ] not in macros :
694
+ lines .append (line )
695
+ lines .append ("" )
696
+ with open (mbed_config_path , "w" ) as fp :
697
+ fp .write ("\n " .join (lines ))
698
+
591
699
592
700
class PlatformIOLibBuilder (LibBuilderBase ):
593
701
@@ -701,10 +809,11 @@ def src_dir(self):
701
809
return self .env .subst ("$PROJECTSRC_DIR" )
702
810
703
811
def get_include_dirs (self ):
704
- include_dirs = LibBuilderBase . get_include_dirs ( self )
812
+ include_dirs = []
705
813
project_include_dir = self .env .subst ("$PROJECTINCLUDE_DIR" )
706
814
if isdir (project_include_dir ):
707
815
include_dirs .append (project_include_dir )
816
+ include_dirs .extend (LibBuilderBase .get_include_dirs (self ))
708
817
return include_dirs
709
818
710
819
def get_search_files (self ):
@@ -772,8 +881,9 @@ def process_dependencies(self): # pylint: disable=too-many-branches
772
881
773
882
def build (self ):
774
883
self ._is_built = True # do not build Project now
884
+ result = LibBuilderBase .build (self )
775
885
self .env .PrependUnique (CPPPATH = self .get_include_dirs ())
776
- return LibBuilderBase . build ( self )
886
+ return result
777
887
778
888
779
889
def GetLibBuilders (env ): # pylint: disable=too-many-branches
0 commit comments