This repository was archived by the owner on Sep 20, 2026. It is now read-only.
Repository navigation
formula: add Mbed-TLS/mbedtls #471
Open
MeteorsLiu
wants to merge
2
commits into
xgo-dev:main
Choose a base branch
from
MeteorsLiu:issue/322-mbedtls-luna
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,153 @@ | ||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| ) | ||
|
|
||
| const consumerSource = `#include "mbedtls/version.h" | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| int main(void) | ||
| { | ||
| char mbedtls_version[18]; | ||
| mbedtls_version_get_string_full(mbedtls_version); | ||
| printf("version: %s\n", mbedtls_version); | ||
| return 0; | ||
| } | ||
| ` | ||
|
|
||
| id "Mbed-TLS/mbedtls" | ||
|
|
||
| fromVer "mbedtls-2.16.12" | ||
|
|
||
| defaults { | ||
| "shared": "OFF", | ||
| "fPIC": "ON", | ||
| "with_zlib": "ON", | ||
| "enable_threading": "OFF", | ||
| } | ||
|
|
||
| filter => { | ||
| for name, values in target.options { | ||
| if name != "shared" && name != "fPIC" && name != "with_zlib" && name != "enable_threading" { | ||
| return false | ||
| } | ||
| for value in values { | ||
| if value != "ON" && value != "OFF" { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| osName := target.require["os"][0] | ||
| if osName == "windows" { | ||
| if target.options["shared"][0] == "ON" || target.options["enable_threading"][0] == "ON" { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| onRequire (proj, deps) => { | ||
| if target.options["with_zlib"][0] == "ON" { | ||
| deps.require "madler/zlib", "v1.3.1" | ||
| } | ||
| } | ||
|
|
||
| onBuild ctx => { | ||
| installDir := ctx.outputDir | ||
| shared := target.options["shared"][0] == "ON" | ||
| fPIC := target.options["fPIC"][0] == "ON" | ||
| withZlib := target.options["with_zlib"][0] == "ON" | ||
| enableThreading := target.options["enable_threading"][0] == "ON" | ||
| osName := target.require["os"][0] | ||
|
|
||
| c := cmake.new(ctx.SourceDir, filepath.join(ctx.SourceDir, "_build"), installDir) | ||
| c.define "CMAKE_INSTALL_LIBDIR", "lib" | ||
| c.define "CMAKE_POLICY_DEFAULT_CMP0042", "NEW" | ||
| c.define "CMAKE_POLICY_VERSION_MINIMUM", "3.5" | ||
| c.defineBool "USE_SHARED_MBEDTLS_LIBRARY", shared | ||
| c.defineBool "USE_STATIC_MBEDTLS_LIBRARY", !shared | ||
| c.defineBool "CMAKE_POSITION_INDEPENDENT_CODE", fPIC | ||
| c.defineBool "ENABLE_ZLIB_SUPPORT", withZlib | ||
| c.defineBool "ENABLE_PROGRAMS", false | ||
| c.defineBool "ENABLE_TESTING", false | ||
| c.defineBool "MBEDTLS_FATAL_WARNINGS", false | ||
| c.defineBool "CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS", true | ||
|
|
||
| cFlags := "" | ||
| if enableThreading { | ||
| if cFlags != "" { | ||
| cFlags += " " | ||
| } | ||
| cFlags += "-DMBEDTLS_THREADING_C=1 -DMBEDTLS_THREADING_PTHREAD=1" | ||
| } | ||
| if cFlags != "" { | ||
| c.define "CMAKE_C_FLAGS", cFlags | ||
| } | ||
|
|
||
| for dep in ctx.Proj.Deps { | ||
| c.use ctx.outputDir(dep) | ||
| } | ||
| c.configure | ||
| c.build | ||
| c.install | ||
|
|
||
| licenseDir := filepath.join(installDir, "licenses") | ||
| os.mkdirAll(licenseDir, 0o755)! | ||
| os.writeFile(filepath.join(licenseDir, "LICENSE"), os.readFile(filepath.join(ctx.SourceDir, "LICENSE"))!, 0o644)! | ||
|
|
||
| libs := ["-L$${libdir}", "-lmbedtls", "-lmbedx509", "-lmbedcrypto"] | ||
| if enableThreading && (osName == "linux" || osName == "freebsd") { | ||
| libs <- "-lpthread" | ||
| } | ||
| requires := []string{} | ||
| if withZlib { | ||
| requires <- "zlib" | ||
| } | ||
|
|
||
| pcDir := filepath.join(installDir, "lib", "pkgconfig") | ||
| os.mkdirAll(pcDir, 0o755)! | ||
| pc := pkgconfig.new( | ||
| name = "mbedtls", | ||
| description = "Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocols.", | ||
| version = "mbedtls-2.16.12", | ||
| requires = requires, | ||
| libs = libs, | ||
| cflags = ["-I$${includedir}"], | ||
| )! | ||
| out := os.create(filepath.join(pcDir, "mbedtls.pc"))! | ||
| pc.writeTo(out)! | ||
| out.close()! | ||
|
|
||
| for dep in ctx.Proj.Deps { | ||
| pkgconfig.use ctx.outputDir(dep) | ||
| } | ||
| pkgconfig.use installDir | ||
| ctx.setMetadata pkgconfig.lookup("mbedtls")! | ||
| } | ||
|
|
||
| onTest ctx => { | ||
| installDir := ctx.outputDir | ||
| testDir := filepath.join(ctx.SourceDir, "_llar_consumer") | ||
| os.mkdirAll(testDir, 0o755)! | ||
|
|
||
| consumer := filepath.join(testDir, "consumer.c") | ||
| os.writeFile(consumer, []byte(consumerSource), 0o644)! | ||
|
|
||
| for dep in ctx.Proj.Deps { | ||
| pkgconfig.use ctx.outputDir(dep) | ||
| } | ||
| pkgconfig.use installDir | ||
| flagsFile := filepath.join(testDir, "mbedtls.flags") | ||
| os.writeFile(flagsFile, []byte(pkgconfig.lookup("mbedtls")!), 0o644)! | ||
|
|
||
| binary := filepath.join(testDir, "consumer") | ||
| cc! consumer, "-o", binary, "@"+flagsFile | ||
|
|
||
| if target.options["shared"][0] == "ON" { | ||
| os.setenv("LD_LIBRARY_PATH", filepath.join(installDir, "lib"))! | ||
| os.setenv("DYLD_LIBRARY_PATH", filepath.join(installDir, "lib"))! | ||
| } | ||
| exec! binary | ||
| } | ||
This file contains hidden or 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,135 @@ | ||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| ) | ||
|
|
||
| const consumerSource = `#include "mbedtls/version.h" | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| int main(void) | ||
| { | ||
| char mbedtls_version[18]; | ||
| mbedtls_version_get_string_full(mbedtls_version); | ||
| printf("version: %s\n", mbedtls_version); | ||
| return 0; | ||
| } | ||
| ` | ||
|
|
||
| id "Mbed-TLS/mbedtls" | ||
|
|
||
| fromVer "mbedtls-3.0.0" | ||
|
|
||
| defaults { | ||
| "shared": "OFF", | ||
| "fPIC": "ON", | ||
| "enable_threading": "OFF", | ||
| } | ||
|
|
||
| filter => { | ||
| for name, values in target.options { | ||
| if name != "shared" && name != "fPIC" && name != "enable_threading" { | ||
| return false | ||
| } | ||
| for value in values { | ||
| if value != "ON" && value != "OFF" { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| osName := target.require["os"][0] | ||
| if osName == "windows" { | ||
| if target.options["shared"][0] == "ON" || target.options["enable_threading"][0] == "ON" { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| onBuild ctx => { | ||
| installDir := ctx.outputDir | ||
| shared := target.options["shared"][0] == "ON" | ||
| fPIC := target.options["fPIC"][0] == "ON" | ||
| enableThreading := target.options["enable_threading"][0] == "ON" | ||
| osName := target.require["os"][0] | ||
|
|
||
| c := cmake.new(ctx.SourceDir, filepath.join(ctx.SourceDir, "_build"), installDir) | ||
| c.define "CMAKE_INSTALL_LIBDIR", "lib" | ||
| c.defineBool "USE_SHARED_MBEDTLS_LIBRARY", shared | ||
| c.defineBool "USE_STATIC_MBEDTLS_LIBRARY", !shared | ||
| c.defineBool "CMAKE_POSITION_INDEPENDENT_CODE", fPIC | ||
| c.defineBool "ENABLE_PROGRAMS", false | ||
| c.defineBool "ENABLE_TESTING", false | ||
| c.defineBool "MBEDTLS_FATAL_WARNINGS", false | ||
| c.defineBool "CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS", true | ||
|
|
||
| cFlags := "" | ||
| if enableThreading { | ||
| if cFlags != "" { | ||
| cFlags += " " | ||
| } | ||
| cFlags += "-DMBEDTLS_THREADING_C=1 -DMBEDTLS_THREADING_PTHREAD=1" | ||
| } | ||
| if cFlags != "" { | ||
| c.define "CMAKE_C_FLAGS", cFlags | ||
| } | ||
|
|
||
| _, err := os.stat(filepath.join(ctx.SourceDir, ".gitmodules")) | ||
| if err == nil { | ||
| git! "submodule", "update", "--init", "--recursive" | ||
| } else if !os.isNotExist(err) { | ||
| panic err | ||
| } | ||
|
|
||
| c.configure | ||
| c.build | ||
| c.install | ||
|
|
||
| licenseDir := filepath.join(installDir, "licenses") | ||
| os.mkdirAll(licenseDir, 0o755)! | ||
| os.writeFile(filepath.join(licenseDir, "LICENSE"), os.readFile(filepath.join(ctx.SourceDir, "LICENSE"))!, 0o644)! | ||
|
|
||
| libs := ["-L$${libdir}", "-lmbedtls", "-lmbedx509", "-lmbedcrypto"] | ||
| if enableThreading && (osName == "linux" || osName == "freebsd") { | ||
| libs <- "-lpthread" | ||
| } | ||
|
|
||
| pcDir := filepath.join(installDir, "lib", "pkgconfig") | ||
| os.mkdirAll(pcDir, 0o755)! | ||
| pc := pkgconfig.new( | ||
| name = "mbedtls", | ||
| description = "Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocols.", | ||
| version = "mbedtls-3.0.0", | ||
| libs = libs, | ||
| cflags = ["-I$${includedir}"], | ||
| )! | ||
| out := os.create(filepath.join(pcDir, "mbedtls.pc"))! | ||
| pc.writeTo(out)! | ||
| out.close()! | ||
|
|
||
| pkgconfig.use installDir | ||
| ctx.setMetadata pkgconfig.lookup("mbedtls")! | ||
| } | ||
|
|
||
| onTest ctx => { | ||
| installDir := ctx.outputDir | ||
| testDir := filepath.join(ctx.SourceDir, "_llar_consumer") | ||
| os.mkdirAll(testDir, 0o755)! | ||
|
|
||
| consumer := filepath.join(testDir, "consumer.c") | ||
| os.writeFile(consumer, []byte(consumerSource), 0o644)! | ||
|
|
||
| pkgconfig.use installDir | ||
| flagsFile := filepath.join(testDir, "mbedtls.flags") | ||
| os.writeFile(flagsFile, []byte(pkgconfig.lookup("mbedtls")!), 0o644)! | ||
|
|
||
| binary := filepath.join(testDir, "consumer") | ||
| cc! consumer, "-o", binary, "@"+flagsFile | ||
|
|
||
| if target.options["shared"][0] == "ON" { | ||
| os.setenv("LD_LIBRARY_PATH", filepath.join(installDir, "lib"))! | ||
| os.setenv("DYLD_LIBRARY_PATH", filepath.join(installDir, "lib"))! | ||
| } | ||
| exec! binary | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Include ws2_32 in Windows link metadata
The Windows filter still permits the default static build, but this combined pkg-config contract advertises only the three Mbed TLS archives. Upstream CMake adds
ws2_32to the library link interface underWIN32; becausepkgconfig.lookupis the metadata consumed by downstream static consumers, Windows users of socket/TLS APIs will otherwise get unresolved Winsock symbols. Add the Windows system library tolibshere and apply the same fix to the 3.x and 4.x thresholds.