Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 35687bc7 authored by Liz Kammer's avatar Liz Kammer
Browse files

Split local/absolute include into attributes

Previously these were expanded into copts, requiring making all includes
absolute and duplicating includes to account for potentially generated
files. We now can handle both of these properly on the Bazel side, so
let's clean up build files a bit.

Test: bp2build.sh
Change-Id: I6c6160738cd6c269408c6c7a37010654d84f3c9d
parent 8337ea4e
Loading
Loading
Loading
Loading
+5 −17
Original line number Diff line number Diff line
@@ -255,13 +255,11 @@ cc_library {
		blueprint: soongCcLibraryPreamble,
		expectedBazelTargets: []string{`cc_library(
    name = "fake-libarm-optimized-routines-math",
    copts = [
        "-Iexternal",
        "-I$(BINDIR)/external",
    ] + select({
    copts = select({
        "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
        "//conditions:default": [],
    }),
    local_includes = ["."],
    srcs_c = ["math/cosf.c"],
)`},
	})
@@ -494,12 +492,9 @@ cc_library_static { name: "android_dep_for_shared" }
		blueprint: soongCcLibraryPreamble,
		expectedBazelTargets: []string{`cc_library(
    name = "a",
    copts = [
        "bothflag",
        "-Ifoo/bar",
        "-I$(BINDIR)/foo/bar",
    ],
    copts = ["bothflag"],
    implementation_deps = [":static_dep_for_both"],
    local_includes = ["."],
    shared = {
        "copts": ["sharedflag"] + select({
            "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
@@ -635,14 +630,7 @@ filegroup {
		blueprint: soongCcLibraryPreamble,
		expectedBazelTargets: []string{`cc_library(
    name = "a",
    asflags = [
        "-Ifoo/bar",
        "-I$(BINDIR)/foo/bar",
    ],
    copts = [
        "-Ifoo/bar",
        "-I$(BINDIR)/foo/bar",
    ],
    local_includes = ["."],
    shared = {
        "srcs": [
            ":shared_filegroup_cpp_srcs",
+0 −4
Original line number Diff line number Diff line
@@ -130,10 +130,6 @@ cc_library_headers {
}`,
		expectedBazelTargets: []string{`cc_library_headers(
    name = "foo_headers",
    copts = [
        "-I.",
        "-I$(BINDIR)/.",
    ],
    export_includes = [
        "dir-1",
        "dir-2",
+28 −44
Original line number Diff line number Diff line
@@ -184,19 +184,13 @@ cc_library_static {
}`,
		expectedBazelTargets: []string{`cc_library_static(
    name = "foo_static",
    absolute_includes = [
        "include_dir_1",
        "include_dir_2",
    ],
    copts = [
        "-Dflag1",
        "-Dflag2",
        "-Iinclude_dir_1",
        "-I$(BINDIR)/include_dir_1",
        "-Iinclude_dir_2",
        "-I$(BINDIR)/include_dir_2",
        "-Ilocal_include_dir_1",
        "-I$(BINDIR)/local_include_dir_1",
        "-Ilocal_include_dir_2",
        "-I$(BINDIR)/local_include_dir_2",
        "-I.",
        "-I$(BINDIR)/.",
    ],
    export_includes = [
        "export_include_dir_1",
@@ -209,6 +203,11 @@ cc_library_static {
        ":static_lib_2",
    ],
    linkstatic = True,
    local_includes = [
        "local_include_dir_1",
        "local_include_dir_2",
        ".",
    ],
    srcs = [
        "foo_static1.cc",
        "foo_static2.cc",
@@ -244,21 +243,16 @@ func TestCcLibraryStaticSubpackage(t *testing.T) {
		blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
    name: "foo_static",
    srcs: [
    ],
    srcs: [],
    include_dirs: [
        "subpackage",
    ],
}`,
		expectedBazelTargets: []string{`cc_library_static(
    name = "foo_static",
    copts = [
        "-Isubpackage",
        "-I$(BINDIR)/subpackage",
        "-I.",
        "-I$(BINDIR)/.",
    ],
    absolute_includes = ["subpackage"],
    linkstatic = True,
    local_includes = ["."],
)`},
	})
}
@@ -347,20 +341,17 @@ cc_library_static {
		blueprint: soongCcLibraryStaticPreamble,
		expectedBazelTargets: []string{`cc_library_static(
    name = "foo_static",
    copts = [
        "-Isubpackage/subsubpackage",
        "-I$(BINDIR)/subpackage/subsubpackage",
        "-Isubpackage2",
        "-I$(BINDIR)/subpackage2",
        "-Isubpackage3/subsubpackage",
        "-I$(BINDIR)/subpackage3/subsubpackage",
        "-Isubpackage/subsubpackage2",
        "-I$(BINDIR)/subpackage/subsubpackage2",
        "-Isubpackage",
        "-I$(BINDIR)/subpackage",
    absolute_includes = [
        "subpackage/subsubpackage",
        "subpackage2",
        "subpackage3/subsubpackage",
    ],
    export_includes = ["./exported_subsubpackage"],
    linkstatic = True,
    local_includes = [
        "subsubpackage2",
        ".",
    ],
)`},
	})
}
@@ -386,13 +377,9 @@ cc_library_static {
}`,
		expectedBazelTargets: []string{`cc_library_static(
    name = "foo_static",
    copts = [
        "-Isubpackage",
        "-I$(BINDIR)/subpackage",
        "-Isubpackage2",
        "-I$(BINDIR)/subpackage2",
    ],
    absolute_includes = ["subpackage"],
    linkstatic = True,
    local_includes = ["subpackage2"],
)`},
	})
}
@@ -420,15 +407,12 @@ cc_library_static {
}`,
		expectedBazelTargets: []string{`cc_library_static(
    name = "foo_static",
    copts = [
        "-Isubpackage",
        "-I$(BINDIR)/subpackage",
        "-Isubpackage2",
        "-I$(BINDIR)/subpackage2",
        "-I.",
        "-I$(BINDIR)/.",
    ],
    absolute_includes = ["subpackage"],
    linkstatic = True,
    local_includes = [
        "subpackage2",
        ".",
    ],
)`},
	})
}
+5 −6
Original line number Diff line number Diff line
@@ -65,10 +65,10 @@ func TestCcObjectSimple(t *testing.T) {
        "-Wno-gcc-compat",
        "-Wall",
        "-Werror",
        "-Iinclude",
        "-I$(BINDIR)/include",
        "-I.",
        "-I$(BINDIR)/.",
    ],
    local_includes = [
        "include",
        ".",
    ],
    srcs = ["a/b/c.c"],
)`,
@@ -113,9 +113,8 @@ cc_defaults {
        "-Wall",
        "-Werror",
        "-fno-addrsig",
        "-I.",
        "-I$(BINDIR)/.",
    ],
    local_includes = ["."],
    srcs = ["a/b/c.c"],
)`,
		}})
+23 −40
Original line number Diff line number Diff line
@@ -216,6 +216,9 @@ type compilerAttributes struct {
	srcs     bazel.LabelListAttribute

	rtti bazel.BoolAttribute

	localIncludes    bazel.StringListAttribute
	absoluteIncludes bazel.StringListAttribute
}

// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
@@ -226,28 +229,8 @@ func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Modul
	var conlyFlags bazel.StringListAttribute
	var cppFlags bazel.StringListAttribute
	var rtti bazel.BoolAttribute

	// Creates the -I flags for a directory, while making the directory relative
	// to the exec root for Bazel to work.
	includeFlags := func(dir string) []string {
		// filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
		moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
		return []string{
			"-I" + moduleDirRootedPath,
			// Include the bindir-rooted path (using make variable substitution). This most
			// closely matches Bazel's native include path handling, which allows for dependency
			// on generated headers in these directories.
			// TODO(b/188084383): Handle local include directories in Bazel.
			"-I$(BINDIR)/" + moduleDirRootedPath,
		}
	}

	// Parse the list of module-relative include directories (-I).
	parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
		// include_dirs are root-relative, not module-relative.
		includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
		return append(includeDirs, baseCompilerProps.Local_include_dirs...)
	}
	var localIncludes bazel.StringListAttribute
	var absoluteIncludes bazel.StringListAttribute

	parseCommandLineFlags := func(soongFlags []string) []string {
		var result []string
@@ -285,19 +268,15 @@ func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Modul

				archVariantCopts := parseCommandLineFlags(baseCompilerProps.Cflags)
				archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
				for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
					archVariantCopts = append(archVariantCopts, includeFlags(dir)...)
					archVariantAsflags = append(archVariantAsflags, includeFlags(dir)...)
				}

				if axis == bazel.NoConfigAxis {
					if includeBuildDirectory(baseCompilerProps.Include_build_directory) {
						flags := includeFlags(".")
						archVariantCopts = append(archVariantCopts, flags...)
						archVariantAsflags = append(archVariantAsflags, flags...)
					}
				localIncludeDirs := baseCompilerProps.Local_include_dirs
				if axis == bazel.NoConfigAxis && includeBuildDirectory(baseCompilerProps.Include_build_directory) {
					localIncludeDirs = append(localIncludeDirs, ".")
				}

				absoluteIncludes.SetSelectValue(axis, config, baseCompilerProps.Include_dirs)
				localIncludes.SetSelectValue(axis, config, localIncludeDirs)

				copts.SetSelectValue(axis, config, archVariantCopts)
				asFlags.SetSelectValue(axis, config, archVariantAsflags)
				conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags))
@@ -308,6 +287,8 @@ func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Modul
	}

	srcs.ResolveExcludes()
	absoluteIncludes.DeduplicateAxesFromBase()
	localIncludes.DeduplicateAxesFromBase()

	productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
		"Cflags":   &copts,
@@ -339,6 +320,8 @@ func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Modul
		conlyFlags:       conlyFlags,
		cppFlags:         cppFlags,
		rtti:             rtti,
		localIncludes:    localIncludes,
		absoluteIncludes: absoluteIncludes,
	}
}

Loading