Skip to content

fix: Replicates SPM default behavior for publicHeadersPath: If this is nil, the directory is set to include #1429

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
12 changes: 6 additions & 6 deletions examples/pkg_manifest_minimal/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "c8ed701b513cf5177118a175d85fbbbcd707ab41",
"version" : "1.3.0"
"revision" : "41982a3656a71c768319979febd796c6fd111d5c",
"version" : "1.5.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log",
"state" : {
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
"revision" : "96a2f8a0fa41e9e09af4585e2724c4e825410b91",
"version" : "1.6.2"
}
},
{
"identity" : "swiftformat",
"kind" : "remoteSourceControl",
"location" : "https://github.com/nicklockwood/SwiftFormat",
"state" : {
"revision" : "607c7057e55cf008e3841696ea7083622e36164f",
"version" : "0.53.2"
"revision" : "468a7d32dedc8d352c191594b3b45d9fd8ba291b",
"version" : "0.55.5"
}
}
],
Expand Down
1 change: 1 addition & 0 deletions examples/pkg_manifest_minimal/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-log", from: "1.6.2"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.55.4"),

.package(path: "third_party/my_local_package"),
.package(path: "third_party/NotThatAmazingModule"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ swift_binary(
visibility = ["//visibility:public"],
deps = [
"//Sources/MyLibrary",
"@swiftpkg_my_local_package//:FarewellFramework",
"@swiftpkg_my_local_package//:GreetingsFramework",
"@swiftpkg_notthatamazingmodule//:NotThatAmazingModule",
"@swiftpkg_swift_argument_parser//:ArgumentParser",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ArgumentParser
import FarewellFramework
import GreetingsFramework
import MyLibrary
import NotThatAmazingModule
Expand All @@ -10,6 +11,8 @@ struct MyExecutable: AsyncParsableCommand {
print(namedGreeting.value)

let complexClass = ComplexClass(name: "Olivia", age: 30, favoriteColors: ["blue"])
print(complexClass.greet())
complexClass.greet()
let farewellMessage = FarewellFramework.myclang_get_farewell_message(MYCLANG_FAREWELL_GOODBYE)
print(String(cString: farewellMessage!))
}
}
13 changes: 9 additions & 4 deletions examples/pkg_manifest_minimal/do_test
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"
bazel="${BIT_BAZEL_BINARY:-bazel}"

assert_match() {
local pattern=${1}
local expected=${1}
local actual="${2}"
local err_msg="Expected to match. pattern: ${pattern}, actual: ${actual}"
[[ "${actual}" =~ ${pattern} ]] || (echo >&2 "${err_msg}" && exit 1)
local err_msg="Expected to match. expected: ${expected}, actual: ${actual}"
if [[ "${expected}" != "${actual}" ]]; then
echo >&2 "${err_msg}"
exit 1
fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

all tests were passed even when they didn't match. Just add a small fix and improve the clarity

Copy link
Owner

Choose a reason for hiding this comment

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

If the pattern check was failing, we should understand why it was failing. The test is using Bash regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have context on that. The other tests in this file aren't running regex match asserts, they use full strings, so I thought the best decision was to keep things simple.

Copy link
Owner

Choose a reason for hiding this comment

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

I have put up #1458 to fix the existing issues with the test. Please revert your changes, removing the assert_match in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice!!! Let me revert my changes

}

# Generate Swift external deps and update build files
Expand All @@ -23,7 +26,9 @@ assert_match() {

# Run MyExecutable target
output="$("${bazel}" run //Sources/MyExecutable)"
assert_match "Good morning, World!" "${output}"
assert_match "Good morning, World!
Hello, my name is Olivia and I'm 30 years old.
Goodbye" "${output}"

# Run old-style executable in my_local_package
output="$("${bazel}" run @swiftpkg_my_local_package//:print-greeting)"
Expand Down
72 changes: 72 additions & 0 deletions examples/pkg_manifest_minimal/res_log.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
name: //Sources/MyExecutable
kind: swift_binary
imports:
- ArgumentParser
- FarewellFramework
- GreetingsFramework
- MyLibrary
- NotThatAmazingModule
local_resolution:
- module: MyLibrary
label: //Sources/MyLibrary
external_resolution:
modules:
- ArgumentParser
- FarewellFramework
- GreetingsFramework
- NotThatAmazingModule
products:
- identity: my_local_package
name: FarewellFramework
label: '@swiftpkg_my_local_package//:FarewellFramework'
- identity: my_local_package
name: GreetingsFramework
label: '@swiftpkg_my_local_package//:GreetingsFramework'
- identity: notthatamazingmodule
name: NotThatAmazingModule
label: '@swiftpkg_notthatamazingmodule//:NotThatAmazingModule'
- identity: swift-argument-parser
name: ArgumentParser
label: '@swiftpkg_swift_argument_parser//:ArgumentParser'
unresolved: []
deps:
- //Sources/MyLibrary
- '@swiftpkg_my_local_package//:FarewellFramework'
- '@swiftpkg_my_local_package//:GreetingsFramework'
- '@swiftpkg_notthatamazingmodule//:NotThatAmazingModule'
- '@swiftpkg_swift_argument_parser//:ArgumentParser'
---
name: //Sources/MyLibrary
kind: swift_library
imports:
- GreetingsFramework
external_resolution:
modules:
- GreetingsFramework
products:
- identity: my_local_package
name: GreetingsFramework
label: '@swiftpkg_my_local_package//:GreetingsFramework'
unresolved: []
deps:
- '@swiftpkg_my_local_package//:GreetingsFramework'
---
name: //Tests/MyLibraryTests
kind: swift_test
imports:
- MyLibrary
- XCTest
builtins:
- XCTest
local_resolution:
- module: MyLibrary
label: //Sources/MyLibrary
external_resolution:
modules:
- XCTest
products: []
unresolved:
- XCTest
deps:
- //Sources/MyLibrary
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ let package = Package(
name: "MyLocalPackage",
products: [
.executable(name: "print-greeting", targets: ["PrintGreeting"]),
.executable(name: "print-farewell", targets: ["PrintFarewell"]),
.library(
name: "GreetingsFramework",
targets: ["GreetingsFramework"]
),
.library(
name: "FarewellFramework",
targets: ["FarewellFramework"]
),
],
targets: [
// Puposefully, using the old-style pattern of a regular target being used by an executable
// product.
.target(
.executableTarget(
name: "PrintGreeting",
dependencies: ["GreetingsFramework"]
),
.executableTarget(
name: "PrintFarewell",
dependencies: ["FarewellFramework"]
),
.target(
name: "GreetingsFramework",
dependencies: []
),
.target(
name: "FarewellFramework",
dependencies: []
),
.testTarget(
name: "GreetingsFrameworkTests",
dependencies: ["GreetingsFramework"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef MYCLANG_LIBRARY_API_H
#define MYCLANG_LIBRARY_API_H

#ifdef __cplusplus
extern "C" {
#endif

/* Version information */
#define MYCLANG_VERSION_MAJOR 1
#define MYCLANG_VERSION_MINOR 0
#define MYCLANG_VERSION_PATCH 0

/* Maximum buffer sizes */
#define MYCLANG_MAX_NAME_LENGTH 256
#define MYCLANG_MAX_BUFFER_SIZE 4096

/* Status codes */
typedef enum {
MYCLANG_SUCCESS = 0,
MYCLANG_ERROR_INVALID_ARGUMENT = -1,
MYCLANG_ERROR_BUFFER_OVERFLOW = -2,
MYCLANG_ERROR_NOT_INITIALIZED = -3
} MyclangStatus;

/* Farewell message types */
typedef enum {
MYCLANG_FAREWELL_GOODBYE = 0,
MYCLANG_FAREWELL_PARTING = 1,
MYCLANG_FAREWELL_SEE_YOU_LATER = 2,
MYCLANG_FAREWELL_TAKE_CARE = 3,
MYCLANG_FAREWELL_ADIEU = 4,
MYCLANG_FAREWELL_DEPARTURE = 5
} MyclangFarewellType;

/* Data structures */
typedef struct {
char name[MYCLANG_MAX_NAME_LENGTH];
unsigned int id;
double value;
} MyclangObject;

/* Function declarations */
MyclangStatus myclang_initialize(void);
MyclangStatus myclang_cleanup(void);

MyclangStatus myclang_create_object(MyclangObject* obj,
const char* name,
unsigned int id,
double value);

MyclangStatus myclang_process_object(const MyclangObject* obj);

const char* myclang_get_version_string(void);

/* Farewell function */
const char* myclang_get_farewell_message(MyclangFarewellType type);

#ifdef __cplusplus
}
#endif

#endif /* MYCLANG_LIBRARY_API_H */
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <string.h>
#include <stdio.h>
#include "MyClangLibrary/API.h"

static int g_initialized = 0;

MyclangStatus myclang_initialize(void) {
if (g_initialized) {
return MYCLANG_ERROR_INVALID_ARGUMENT;
}
g_initialized = 1;
return MYCLANG_SUCCESS;
}

MyclangStatus myclang_cleanup(void) {
if (!g_initialized) {
return MYCLANG_ERROR_NOT_INITIALIZED;
}
g_initialized = 0;
return MYCLANG_SUCCESS;
}

MyclangStatus myclang_create_object(MyclangObject* obj,
const char* name,
unsigned int id,
double value) {
if (!g_initialized) {
return MYCLANG_ERROR_NOT_INITIALIZED;
}

if (!obj || !name) {
return MYCLANG_ERROR_INVALID_ARGUMENT;
}

if (strlen(name) >= MYCLANG_MAX_NAME_LENGTH) {
return MYCLANG_ERROR_BUFFER_OVERFLOW;
}

strncpy(obj->name, name, MYCLANG_MAX_NAME_LENGTH - 1);
obj->name[MYCLANG_MAX_NAME_LENGTH - 1] = '\0';
obj->id = id;
obj->value = value;

return MYCLANG_SUCCESS;
}

MyclangStatus myclang_process_object(const MyclangObject* obj) {
if (!g_initialized) {
return MYCLANG_ERROR_NOT_INITIALIZED;
}

if (!obj) {
return MYCLANG_ERROR_INVALID_ARGUMENT;
}

// Example processing - in real implementation, this would do something useful
return MYCLANG_SUCCESS;
}

const char* myclang_get_version_string(void) {
static char version[32];
snprintf(version, sizeof(version), "%d.%d.%d",
MYCLANG_VERSION_MAJOR,
MYCLANG_VERSION_MINOR,
MYCLANG_VERSION_PATCH);
return version;
}

const char* myclang_get_farewell_message(MyclangFarewellType type) {
switch (type) {
case MYCLANG_FAREWELL_GOODBYE:
return "Goodbye";
case MYCLANG_FAREWELL_PARTING:
return "Parting";
case MYCLANG_FAREWELL_SEE_YOU_LATER:
return "See you later";
case MYCLANG_FAREWELL_TAKE_CARE:
return "Take care";
case MYCLANG_FAREWELL_ADIEU:
return "Adieu";
case MYCLANG_FAREWELL_DEPARTURE:
return "Departure";
default:
return "Goodbye";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import FarewellFramework

let message = FarewellFramework.myclang_get_farewell_message(MYCLANG_FAREWELL_SEE_YOU_LATER)

let swiftString = String(cString: message!)

print(swiftString)
9 changes: 9 additions & 0 deletions swiftpkg/internal/pkginfos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,20 @@ def _new_clang_src_info_from_sources(
paths.join(pkg_path, target_path),
)

# If the Swift package manifest does not specify a public headers path,
# use the default "include" directory, if it exists.
# This copies the behavior of the canonical Swift Package Manager implementation.
# https://developer.apple.com/documentation/packagedescription/target/publicheaderspath
public_includes = []
if public_hdrs_path != None:
public_includes.append(
paths.normalize(paths.join(abs_target_path, public_hdrs_path)),
)
elif repository_files.path_exists(
repository_ctx,
paths.join(abs_target_path, "include"),
):
public_includes.append(paths.join(abs_target_path, "include"))

# If the Swift package manifest has explicit source paths, respect them.
# (Be sure to include any explicitly specified include directories.)
Expand Down
1 change: 0 additions & 1 deletion swiftpkg/tests/swiftpkg_build_files_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ _pkg_info = pkginfos.new(
exclude_paths = [
"src/do_not_include_me.cc",
],
public_hdrs_path = "include",
dependencies = [],
clang_settings = pkginfos.new_clang_settings([
pkginfos.new_build_setting(
Expand Down