github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/soong/cc/stl.go (about)

     1  // Copyright 2016 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cc
    16  
    17  import (
    18  	"android/soong/android"
    19  	"fmt"
    20  )
    21  
    22  func getNdkStlFamily(ctx android.ModuleContext, m *Module) string {
    23  	stl := m.stl.Properties.SelectedStl
    24  	switch stl {
    25  	case "ndk_libc++_shared", "ndk_libc++_static":
    26  		return "libc++"
    27  	case "ndk_system":
    28  		return "system"
    29  	case "":
    30  		return "none"
    31  	default:
    32  		ctx.ModuleErrorf("stl: %q is not a valid STL", stl)
    33  		return ""
    34  	}
    35  }
    36  
    37  type StlProperties struct {
    38  	// Select the STL library to use.  Possible values are "libc++",
    39  	// "libc++_static", "libstdc++", or "none". Leave blank to select the
    40  	// default.
    41  	Stl *string `android:"arch_variant"`
    42  
    43  	SelectedStl string `blueprint:"mutated"`
    44  }
    45  
    46  type stl struct {
    47  	Properties StlProperties
    48  }
    49  
    50  func (stl *stl) props() []interface{} {
    51  	return []interface{}{&stl.Properties}
    52  }
    53  
    54  func (stl *stl) begin(ctx BaseModuleContext) {
    55  	stl.Properties.SelectedStl = func() string {
    56  		s := ""
    57  		if stl.Properties.Stl != nil {
    58  			s = *stl.Properties.Stl
    59  		}
    60  		if ctx.useSdk() && ctx.Device() {
    61  			switch s {
    62  			case "":
    63  				return "ndk_system"
    64  			case "c++_shared", "c++_static":
    65  				return "ndk_lib" + s
    66  			case "libc++":
    67  				return "ndk_libc++_shared"
    68  			case "libc++_static":
    69  				return "ndk_libc++_static"
    70  			case "none":
    71  				return ""
    72  			default:
    73  				ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", s)
    74  				return ""
    75  			}
    76  		} else if ctx.Windows() {
    77  			switch s {
    78  			case "libc++", "libc++_static", "libstdc++", "":
    79  				// libc++ is not supported on mingw
    80  				return "libstdc++"
    81  			case "none":
    82  				return ""
    83  			default:
    84  				ctx.ModuleErrorf("stl: %q is not a supported STL for windows", s)
    85  				return ""
    86  			}
    87  		} else {
    88  			switch s {
    89  			case "libc++", "libc++_static":
    90  				return s
    91  			case "none":
    92  				return ""
    93  			case "":
    94  				if ctx.static() {
    95  					return "libc++_static"
    96  				} else {
    97  					return "libc++"
    98  				}
    99  			default:
   100  				ctx.ModuleErrorf("stl: %q is not a supported STL", s)
   101  				return ""
   102  			}
   103  		}
   104  	}()
   105  }
   106  
   107  func (stl *stl) deps(ctx BaseModuleContext, deps Deps) Deps {
   108  	switch stl.Properties.SelectedStl {
   109  	case "libstdc++":
   110  		// Nothing
   111  	case "libc++", "libc++_static":
   112  		if stl.Properties.SelectedStl == "libc++" {
   113  			deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
   114  		} else {
   115  			deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
   116  		}
   117  		if ctx.toolchain().Bionic() {
   118  			if ctx.Arch().ArchType == android.Arm {
   119  				deps.StaticLibs = append(deps.StaticLibs, "libunwind_llvm")
   120  			}
   121  			if ctx.staticBinary() {
   122  				deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
   123  			}
   124  		}
   125  	case "":
   126  		// None or error.
   127  	case "ndk_system":
   128  		// TODO: Make a system STL prebuilt for the NDK.
   129  		// The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
   130  		// its own includes. The includes are handled in CCBase.Flags().
   131  		deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
   132  	case "ndk_libc++_shared":
   133  		deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
   134  	case "ndk_libc++_static":
   135  		deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
   136  	default:
   137  		panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
   138  	}
   139  
   140  	return deps
   141  }
   142  
   143  func (stl *stl) flags(ctx ModuleContext, flags Flags) Flags {
   144  	switch stl.Properties.SelectedStl {
   145  	case "libc++", "libc++_static":
   146  		flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
   147  
   148  		if ctx.Darwin() {
   149  			// libc++'s headers are annotated with availability macros that
   150  			// indicate which version of Mac OS was the first to ship with a
   151  			// libc++ feature available in its *system's* libc++.dylib. We do
   152  			// not use the system's library, but rather ship our own. As such,
   153  			// these availability attributes are meaningless for us but cause
   154  			// build breaks when we try to use code that would not be available
   155  			// in the system's dylib.
   156  			flags.CppFlags = append(flags.CppFlags,
   157  				"-D_LIBCPP_DISABLE_AVAILABILITY")
   158  		}
   159  
   160  		if !ctx.toolchain().Bionic() {
   161  			flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
   162  			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
   163  			if ctx.staticBinary() {
   164  				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
   165  			} else {
   166  				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
   167  			}
   168  		} else {
   169  			if ctx.Arch().ArchType == android.Arm {
   170  				flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
   171  			}
   172  		}
   173  	case "libstdc++":
   174  		// Nothing
   175  	case "ndk_system":
   176  		ndkSrcRoot := android.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
   177  		flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
   178  	case "ndk_libc++_shared", "ndk_libc++_static":
   179  		// Nothing.
   180  	case "":
   181  		// None or error.
   182  		if !ctx.toolchain().Bionic() {
   183  			flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
   184  			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
   185  			if ctx.staticBinary() {
   186  				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
   187  			} else {
   188  				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
   189  			}
   190  		}
   191  	default:
   192  		panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
   193  	}
   194  
   195  	return flags
   196  }
   197  
   198  var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string
   199  
   200  func init() {
   201  	hostDynamicGccLibs = map[android.OsType][]string{
   202  		android.Linux:  []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
   203  		android.Darwin: []string{"-lc", "-lSystem"},
   204  		android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
   205  			"-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
   206  			"-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
   207  			"-lmsvcrt"},
   208  	}
   209  	hostStaticGccLibs = map[android.OsType][]string{
   210  		android.Linux:   []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
   211  		android.Darwin:  []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
   212  		android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
   213  	}
   214  }