1// Copyright 2015 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
15package cc
16
17import (
18	"fmt"
19	"io"
20	"path/filepath"
21	"strings"
22
23	"android/soong/common"
24)
25
26func (c *Module) AndroidMk() (ret common.AndroidMkData, err error) {
27	ret.OutputFile = c.outputFile
28	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) (err error) {
29		if len(c.deps.SharedLibs) > 0 {
30			fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.deps.SharedLibs, " "))
31		}
32		return nil
33	})
34
35	callSubAndroidMk := func(obj interface{}) {
36		if obj != nil {
37			if androidmk, ok := obj.(interface {
38				AndroidMk(*common.AndroidMkData)
39			}); ok {
40				androidmk.AndroidMk(&ret)
41			}
42		}
43	}
44
45	for _, feature := range c.features {
46		callSubAndroidMk(feature)
47	}
48
49	callSubAndroidMk(c.compiler)
50	callSubAndroidMk(c.linker)
51	callSubAndroidMk(c.installer)
52
53	return ret, nil
54}
55
56func (library *baseLinker) AndroidMk(ret *common.AndroidMkData) {
57	if library.static() {
58		ret.Class = "STATIC_LIBRARIES"
59	} else {
60		ret.Class = "SHARED_LIBRARIES"
61	}
62}
63
64func (library *libraryLinker) AndroidMk(ret *common.AndroidMkData) {
65	library.baseLinker.AndroidMk(ret)
66
67	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) error {
68		exportedIncludes := library.exportedFlags()
69		for _, flag := range library.exportedFlags() {
70			if flag != "" {
71				exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
72			}
73		}
74		if len(exportedIncludes) > 0 {
75			fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
76		}
77
78		fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
79
80		// These are already included in LOCAL_SHARED_LIBRARIES
81		fmt.Fprintln(w, "LOCAL_CXX_STL := none")
82		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
83
84		return nil
85	})
86}
87
88func (object *objectLinker) AndroidMk(ret *common.AndroidMkData) {
89	ret.Custom = func(w io.Writer, name, prefix string) error {
90		out := ret.OutputFile.Path()
91
92		fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String(), "| $(ACP)")
93		fmt.Fprintln(w, "\t$(copy-file-to-target)")
94
95		return nil
96	}
97}
98
99func (binary *binaryLinker) AndroidMk(ret *common.AndroidMkData) {
100	ret.Class = "EXECUTABLES"
101	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) error {
102		fmt.Fprintln(w, "LOCAL_CXX_STL := none")
103		fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
104		return nil
105	})
106}
107
108func (test *testLinker) AndroidMk(ret *common.AndroidMkData) {
109	test.binaryLinker.AndroidMk(ret)
110	if Bool(test.Properties.Test_per_src) {
111		ret.SubName = test.binaryLinker.Properties.Stem
112	}
113}
114
115func (installer *baseInstaller) AndroidMk(ret *common.AndroidMkData) {
116	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile common.Path) error {
117		path := installer.path.RelPathString()
118		dir, file := filepath.Split(path)
119		stem := strings.TrimSuffix(file, filepath.Ext(file))
120		fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+dir)
121		fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
122		return nil
123	})
124}
125