@@ -506,7 +506,7 @@ class Build
506506 def env_CFLAGS
507507 return @env_CFLAGS if @env_CFLAGS
508508
509- env = [ ]
509+ env = ENV . fetch ( 'CFLAGS' , '' ) . split
510510
511511 env << '-O2'
512512
@@ -532,13 +532,29 @@ class Build
532532 env += ENV [ 'NIX_CFLAGS_COMPILE' ] . split
533533 end
534534
535- @env_CFLAGS = env
535+ clean_env = [ ]
536+ skip_next = false
537+ env . each_with_index do |flag , index |
538+ if skip_next
539+ skip_next = false
540+ next
541+ end
542+
543+ if flag == '-isystem' && env . size > index + 1
544+ clean_env << "-isystem #{ env [ index + 1 ] } "
545+ skip_next = true
546+ else
547+ clean_env << flag
548+ end
549+ end
550+
551+ @env_CFLAGS = clean_env . map ( &:strip ) . reject ( &:empty? ) . compact # .uniq
536552 end
537553
538554 def env_LDFLAGS
539555 return @env_LDFLAGS if @env_LDFLAGS
540556
541- env = [ ]
557+ env = ENV . fetch ( 'LDFLAGS' , '' ) . split
542558
543559 # Ensure library re-linking and code signing will work after building.
544560 env << '-Wl,-headerpad_max_install_names'
@@ -555,13 +571,13 @@ class Build
555571
556572 env += ENV [ 'NIX_LDFLAGS' ] . split if use_nix? && ENV [ 'NIX_LDFLAGS' ]
557573
558- @env_LDFLAGS = env
574+ @env_LDFLAGS = env . map ( & :strip ) . reject ( & :empty? ) . compact # .uniq
559575 end
560576
561577 def env_LIBRARY_PATH
562578 return @env_LIBRARY_PATH if @env_LIBRARY_PATH
563579
564- env = [ ]
580+ env = ENV . fetch ( 'LIBRARY_PATH' , '' ) . split
565581
566582 if options [ :native_comp ]
567583 env += [
@@ -573,37 +589,49 @@ class Build
573589
574590 env << '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib'
575591
576- @env_LIBRARY_PATH = env
592+ @env_LIBRARY_PATH = env . map ( & :strip ) . reject ( & :empty? ) . compact . uniq
577593 end
578594
579595 def env_PKG_CONFIG_PATH
580- return [ ] if use_nix?
596+ return @env_PKG_CONFIG_PATH if @env_PKG_CONFIG_PATH
581597
582- @env_PKG_CONFIG_PATH ||= [
583- File . join ( brew_dir , 'lib/pkgconfig' ) ,
584- File . join ( brew_dir , 'share/pkgconfig' ) ,
585- File . join ( brew_dir , 'opt/expat/lib/pkgconfig' ) ,
586- File . join ( brew_dir , 'opt/libxml2/lib/pkgconfig' ) ,
587- File . join ( brew_dir , 'opt/ncurses/lib/pkgconfig' ) ,
588- File . join ( brew_dir , 'opt/zlib/lib/pkgconfig' ) ,
589- File . join (
590- brew_dir ,
591- 'Homebrew/Library/Homebrew/os/mac/pkgconfig' ,
592- OS . version . to_s
593- )
594- ]
598+ env = ENV . fetch ( 'PKG_CONFIG_PATH' , '' ) . split
599+
600+ unless use_nix?
601+ env += [
602+ File . join ( brew_dir , 'lib/pkgconfig' ) ,
603+ File . join ( brew_dir , 'share/pkgconfig' ) ,
604+ File . join ( brew_dir , 'opt/expat/lib/pkgconfig' ) ,
605+ File . join ( brew_dir , 'opt/libxml2/lib/pkgconfig' ) ,
606+ File . join ( brew_dir , 'opt/ncurses/lib/pkgconfig' ) ,
607+ File . join ( brew_dir , 'opt/zlib/lib/pkgconfig' ) ,
608+ File . join (
609+ brew_dir ,
610+ 'Homebrew/Library/Homebrew/os/mac/pkgconfig' ,
611+ OS . version . to_s
612+ )
613+ ]
614+ end
615+
616+ @env_PKG_CONFIG_PATH = env . map ( &:strip ) . reject ( &:empty? ) . compact . uniq
595617 end
596618
597619 def env_PATH
598- return [ ] if use_nix?
620+ return @env_PATH if @env_PATH
599621
600- @env_PATH ||= [
601- File . join ( brew_dir , 'opt/make/libexec/gnubin' ) ,
602- File . join ( brew_dir , 'opt/coreutils/libexec/gnubin' ) ,
603- File . join ( brew_dir , 'opt/gnu-sed/libexec/gnubin' ) ,
604- File . join ( brew_dir , 'bin' ) ,
605- File . join ( brew_dir , 'opt/texinfo/bin' )
606- ]
622+ env = ENV . fetch ( 'PATH' , '' ) . split
623+
624+ unless use_nix?
625+ env += [
626+ File . join ( brew_dir , 'opt/make/libexec/gnubin' ) ,
627+ File . join ( brew_dir , 'opt/coreutils/libexec/gnubin' ) ,
628+ File . join ( brew_dir , 'opt/gnu-sed/libexec/gnubin' ) ,
629+ File . join ( brew_dir , 'bin' ) ,
630+ File . join ( brew_dir , 'opt/texinfo/bin' )
631+ ]
632+ end
633+
634+ @env_PATH = env . map ( &:strip ) . reject ( &:empty? ) . compact
607635 end
608636 # rubocop:enable Naming/MethodName,Naming/VariableName
609637
@@ -612,22 +640,14 @@ class Build
612640
613641 env = {
614642 'CC' => use_nix? ? 'clang' : '/usr/bin/clang' ,
615- 'PATH' => [
616- env_PATH , ENV . fetch ( 'PATH' , nil )
617- ] . flatten . compact . reject ( &:empty? ) . join ( ':' ) ,
618- 'PKG_CONFIG_PATH' => [
619- env_PKG_CONFIG_PATH ,
620- ENV . fetch ( 'PKG_CONFIG_PATH' , nil )
621- ] . flatten . compact . reject ( &:empty? ) . join ( ':' )
643+ 'PATH' => env_PATH . join ( ':' ) ,
644+ 'PKG_CONFIG_PATH' => env_PKG_CONFIG_PATH . join ( ':' )
622645 }
623646
624647 if options [ :native_comp ]
625- env [ 'CFLAGS' ] = [ env_CFLAGS , ENV . fetch ( 'CFLAGS' , nil ) ]
626- . flatten . compact . reject ( &:empty? ) . join ( ' ' )
627- env [ 'LDFLAGS' ] = [ env_LDFLAGS , ENV . fetch ( 'LDFLAGS' , nil ) ]
628- . flatten . compact . reject ( &:empty? ) . join ( ' ' )
629- env [ 'LIBRARY_PATH' ] = [ env_LIBRARY_PATH , ENV . fetch ( 'LIBRARY_PATH' , nil ) ]
630- . flatten . compact . reject ( &:empty? ) . join ( ':' )
648+ env [ 'CFLAGS' ] = env_CFLAGS . join ( ' ' )
649+ env [ 'LDFLAGS' ] = env_LDFLAGS . join ( ' ' )
650+ env [ 'LIBRARY_PATH' ] = env_LIBRARY_PATH . join ( ':' )
631651 end
632652
633653 @compile_env = env
0 commit comments