Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables C++ support in srcs #550

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1022,14 +1022,15 @@ def apple_library(name, library_tools = {}, export_private_headers = True, names
_append_headermap_copts(swift_angle_bracket_hmap_name, "-I", additional_objc_copts, additional_swift_copts, additional_cc_copts)

# Note: this line is intentionally disabled
if cpp_sources and False:
if cpp_sources:
additional_cc_copts.append("-I.")
native.cc_library(
native.objc_library(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had to be an objc_library to avoid an error in framework_packaging_impl where it expected all deps would have an Objc provider

name = cpp_libname,
srcs = cpp_sources + objc_private_hdrs,
srcs = cpp_sources + objc_private_hdrs + objc_non_exported_hdrs,
hdrs = objc_hdrs,
copts = copts_by_build_setting.cc_copts + cc_copts + additional_cc_copts,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a number of flags: perhaps you can add as a followup.


    additional_objc_vfs_deps = select({
        "@build_bazel_rules_ios//:virtualize_frameworks": [framework_vfs_overlay_name_swift] + [framework_vfs_overlay_name],
        "//conditions:default": [framework_vfs_overlay_name_swift] + [framework_vfs_overlay_name] if enable_framework_vfs else [],
    })
    additional_objc_vfs_copts = select({
        "@build_bazel_rules_ios//:virtualize_frameworks": framework_vfs_objc_copts,
        "//conditions:default": framework_vfs_objc_copts if enable_framework_vfs else [],
    })

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A follow up is a good idea 👍

deps = deps,
deps = deps + private_deps,
defines = defines,
tags = tags_manual,
testonly = kwargs.get("testonly", False),
)
Expand Down
56 changes: 56 additions & 0 deletions tests/ios/frameworks/objcpp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
load("//rules:framework.bzl", "apple_framework")
load("//rules:test.bzl", "ios_unit_test")

apple_framework(
name = "ObjCppMixedLib",
srcs = glob(
[
"ObjCppMixedLib/*.mm",
"ObjCppMixedLib/*.cpp",
],
),
objc_copts = [
"-fmodules",
"-fcxx-modules",
],
platforms = {"ios": "10.0"},
public_headers = glob(
[
"ObjCppMixedLib/A.h",
"ObjCppMixedLib/B.hpp",
],
),
visibility = ["//visibility:public"],
)

apple_framework(
name = "ObjCLib",
srcs = glob(
[
"ObjCLib/*.m",
],
),
platforms = {"ios": "10.0"},
public_headers = glob(
[
"ObjCLib/C.h",
],
),
visibility = ["//visibility:public"],
deps = [":ObjCppMixedLib"],
)

ios_unit_test(
name = "ObjCppMixedLibTests",
srcs = glob(
[
"ObjCppMixedLibTests/*.m",
],
),
minimum_os_version = "10.0",
visibility = ["//visibility:public"],
deps = [
":ObjCLib",
":ObjCppMixedLib",
],
)
5 changes: 5 additions & 0 deletions tests/ios/frameworks/objcpp/ObjCLib/C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import Foundation;

@interface C : NSObject

@end
16 changes: 16 additions & 0 deletions tests/ios/frameworks/objcpp/ObjCLib/C.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import ObjCppMixedLib;
#import "C.h"

@implementation C

- (instancetype)init
{
self = [super init];
if (self) {
A *a = [A new];
[a foo];
}
return self;
}

@end
7 changes: 7 additions & 0 deletions tests/ios/frameworks/objcpp/ObjCppMixedLib/A.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import Foundation;

@interface A : NSObject

- (void)foo;

@end
21 changes: 21 additions & 0 deletions tests/ios/frameworks/objcpp/ObjCppMixedLib/A.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#import "A.h"
#import "B.hpp"

using namespace dummy;

@implementation A

- (instancetype)init
{
self = [super init];
if (self) {
dummy::bar();
}
return self;
}

- (void)foo
{
}

@end
6 changes: 6 additions & 0 deletions tests/ios/frameworks/objcpp/ObjCppMixedLib/B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace dummy
{
void bar()
{
}
}
13 changes: 13 additions & 0 deletions tests/ios/frameworks/objcpp/ObjCppMixedLib/B.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _DUMMY_H
#define _DUMMY_H

#ifdef __cplusplus

namespace dummy
{
void bar();
}

#endif // __cplusplus

#endif // DUMMY_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@import ObjCppMixedLib;
@import ObjCLib;
@import XCTest;

@interface ObjCppMixedLibTests : XCTestCase
@end

@implementation ObjCppMixedLibTests

- (void)test_objcMixedLib
{
A *a = [A new];
XCTAssertNotNil(a);
C *c = [C new];
XCTAssertNotNil(c);
}

@end