forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std.Build: add an option to CSourceFile to override the language dete…
…ction. It is normally based on the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
- Loading branch information
Showing
8 changed files
with
182 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const test_step = b.step("test", "Test it"); | ||
b.default_step = test_step; | ||
|
||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
// single-file c-header library | ||
{ | ||
const exe = b.addExecutable(.{ | ||
.name = "single-file-library", | ||
.root_source_file = b.path("main.zig"), | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
exe.linkLibC(); | ||
exe.addIncludePath(b.path(".")); | ||
exe.addCSourceFile(.{ | ||
.file = b.path("single_file_library.h"), | ||
.lang = .c, | ||
.flags = &.{"-DTSTLIB_IMPLEMENTATION"}, | ||
}); | ||
|
||
test_step.dependOn(&b.addRunArtifact(exe).step); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const std = @import("std"); | ||
const C = @cImport({ | ||
@cInclude("single_file_library.h"); | ||
}); | ||
|
||
pub fn main() !void { | ||
const msg = "hello"; | ||
const val = C.tstlib_len(msg); | ||
if (val != msg.len) | ||
std.process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// library header: | ||
extern unsigned tstlib_len(const char* msg); | ||
|
||
// library implementation: | ||
#ifdef TSTLIB_IMPLEMENTATION | ||
|
||
#include <string.h> | ||
|
||
unsigned tstlib_len(const char* msg) | ||
{ | ||
return strlen(msg); | ||
} | ||
|
||
#endif |