Skip to content

Commit 642157b

Browse files
authored
Add support for C/C++, improve Obj-C handling (#129)
* Expand list of C extensions, ignore CppCompile for now * Add support for C/C++ * Add support for specifying dependency rules to filter for * Lint files * Add more supported C++ file types
1 parent d2350c1 commit 642157b

26 files changed

+555
-316
lines changed

Example/HelloWorld/BUILD

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application", "ios_unit_tes
33
load("@build_bazel_rules_apple//apple:macos.bzl", "macos_application", "macos_command_line_application", "macos_unit_test")
44
load("@build_bazel_rules_apple//apple:watchos.bzl", "watchos_application", "watchos_extension", "watchos_unit_test")
55
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_interop_hint", "swift_library")
6+
load("@rules_cc//cc:defs.bzl", "cc_library")
67
load("@sourcekit_bazel_bsp//rules:setup_sourcekit_bsp.bzl", "setup_sourcekit_bsp")
78
load("//tools:apple.bzl", "IOS_MINIMUM_OS_VERSION", "WATCHOS_MINIMUM_OS_VERSION", "MACOS_MINIMUM_OS_VERSION")
89

@@ -33,12 +34,29 @@ objc_library(
3334
name = "TodoObjCSupport",
3435
srcs = glob(
3536
[
36-
"TodoObjCSupport/Sources/*.h",
37+
"TodoObjCSupport/Sources/*.mm",
3738
"TodoObjCSupport/Sources/*.m",
3839
],
3940
),
4041
hdrs = glob(["TodoObjCSupport/Sources/*.h"]),
4142
aspect_hints = [":TodoObjCSupport_hint"],
43+
deps = [":TodoCSupport"],
44+
)
45+
46+
cc_library(
47+
name = "TodoCSupport",
48+
srcs = glob(
49+
[
50+
"TodoCSupport/src/*.cpp",
51+
"TodoCSupport/src/*.c",
52+
],
53+
),
54+
hdrs = glob(
55+
[
56+
"TodoCSupport/include/*.hpp",
57+
"TodoCSupport/include/*.h",
58+
],
59+
),
4260
)
4361

4462
swift_library(

Example/HelloWorld/HelloWorldLib/Sources/TodoItemRow.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ struct TodoItemRow: View {
4242
.foregroundColor(item.isCompleted ? .gray : .primary)
4343
.animation(.easeInOut(duration: 0.2), value: item.isCompleted)
4444

45-
Text(SKDateDistanceCalculator.humanReadableDistance(fromNow: item.createdAt))
45+
Text(SKObjCppUtils.humanReadableDistance(fromNow: item.createdAt))
4646
.font(.caption)
4747
.foregroundColor(.gray)
48+
.onAppear {
49+
print(SKObjCUtils.greetingFromC() as Any)
50+
print(SKObjCUtils.multiply(withC: 2, b: 2))
51+
}
4852
}
4953

5054
Spacer()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
#ifndef SK_C_UTILS_H
21+
#define SK_C_UTILS_H
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
const char* sk_c_get_greeting(void);
28+
int sk_c_multiply(int a, int b);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
34+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef SK_CPP_UTILS_HPP
2+
#define SK_CPP_UTILS_HPP
3+
4+
#include <string>
5+
6+
namespace SKCppUtils {
7+
8+
std::string getGreeting();
9+
int add(int a, int b);
10+
11+
} // namespace SKCppUtils
12+
13+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
#include "../include/SKCUtils.h"
21+
22+
const char* sk_c_get_greeting(void) {
23+
return "Hello from C!";
24+
}
25+
26+
int sk_c_multiply(int a, int b) {
27+
return a * b;
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "../include/SKCppUtils.hpp"
2+
3+
namespace SKCppUtils {
4+
5+
std::string getGreeting() {
6+
return "Hello from C++!";
7+
}
8+
9+
int add(int a, int b) {
10+
return a + b;
11+
}
12+
13+
} // namespace SKCppUtils
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
#import <Foundation/Foundation.h>
21+
22+
@interface SKObjCUtils : NSObject
23+
24+
+ (NSString *)greetingFromC;
25+
+ (NSInteger)multiplyWithC:(NSInteger)a b:(NSInteger)b;
26+
27+
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
#import "HelloWorld/TodoObjCSupport/Sources/SKObjCUtils.h"
21+
#import "HelloWorld/TodoCSupport/include/SKCUtils.h"
22+
23+
@implementation SKObjCUtils
24+
25+
+ (NSString *)greetingFromC {
26+
const char *cGreeting = sk_c_get_greeting();
27+
return [NSString stringWithUTF8String:cGreeting];
28+
}
29+
30+
+ (NSInteger)multiplyWithC:(NSInteger)a b:(NSInteger)b {
31+
return sk_c_multiply((int)a, (int)b);
32+
}
33+
34+
@end

Example/HelloWorld/TodoObjCSupport/Sources/SKDateDistanceCalculator.h renamed to Example/HelloWorld/TodoObjCSupport/Sources/SKObjCppUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#import <Foundation/Foundation.h>
2121

22-
@interface SKDateDistanceCalculator : NSObject
22+
@interface SKObjCppUtils : NSObject
2323

2424
/**
2525
* Calculates the time interval between the given date and now

Example/HelloWorld/TodoObjCSupport/Sources/SKDateDistanceCalculator.m renamed to Example/HelloWorld/TodoObjCSupport/Sources/SKObjCppUtils.mm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
// specific language governing permissions and limitations
1818
// under the License.
1919

20-
#import "HelloWorld/TodoObjCSupport/Sources/SKDateDistanceCalculator.h"
20+
#import "HelloWorld/TodoObjCSupport/Sources/SKObjCppUtils.h"
21+
#import "HelloWorld/TodoCSupport/include/SKCppUtils.hpp"
2122

22-
@implementation SKDateDistanceCalculator
23+
@implementation SKObjCppUtils
2324

2425
+ (NSTimeInterval)distanceFromNow:(NSDate *)date {
2526
if (!date) {
2627
return 0.0;
2728
}
2829

30+
NSLog(@"%s", SKCppUtils::getGreeting().c_str());
31+
NSLog(@"%ld", (long)SKCppUtils::add(1, 2));
32+
2933
NSDate *now = [NSDate date];
3034
return [now timeIntervalSinceDate:date];
3135
}

0 commit comments

Comments
 (0)