Skip to content
Open
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
91 changes: 91 additions & 0 deletions json-c/json-c/json-c-0.18-20240915/Jsonc_llar.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import (
"os"
"strings"
)

id "json-c/json-c"

fromVer "json-c-0.18-20240915"

onBuild ctx => {
installDir := ctx.outputDir
c := cmake.new(ctx.SourceDir, ctx.SourceDir+"/_build", installDir)

// Keep a standard lib/ layout so the installed pkg-config file and the
// dependency search paths resolve without lib64 handling.
c.define "CMAKE_INSTALL_LIBDIR", "lib"
// Build the static archive with position-independent code, matching the
// Conan recipe defaults (shared=False, fPIC=True). json-c builds its
// shared and static libraries from independent targets, so select the
// static one and leave DISABLE_STATIC_FPIC off to keep -fPIC.
c.defineBool "BUILD_SHARED_LIBS", false
c.defineBool "BUILD_STATIC_LIBS", true
c.defineBool "DISABLE_STATIC_FPIC", false
// Library-only install: skip the upstream test suite and CLI apps.
c.defineBool "BUILD_TESTING", false
c.defineBool "BUILD_APPS", false

c.configure
c.build
c.install

// Derive consumer flags from the installed pkg-config file. The static
// query also reports json-c's private math dependency (-lm on UNIX),
// which a static consumer must link.
c.use installDir
capout => {
exec "pkg-config", "--cflags", "--libs", "--static", "json-c"
}
if lastErr != nil {
panic lastErr
}
ctx.setMetadata strings.trimSpace(output)
}

onTest ctx => {
installDir := ctx.outputDir

// Build the consumer in a tree independent of the onBuild scratch tree so

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

onTest synthesizes consumer.c and CMakeLists.txt and writes them into ctx.SourceDir (testDir := ctx.SourceDir + "/_llar_consumer"), mutating the temporary upstream checkout. The reference's "Installed-Output Tests" section says test source files should come from the fresh ctx.SourceDir checkout or from files shipped in the Formula module (read via ctx.Proj.readFile); the example treats ctx.SourceDir + "/consumer" as a pre-existing subdir, not a place to generate files. Consider shipping consumer.c/CMakeLists.txt in the Formula module (read with ctx.Proj.readFile) or writing them to a scratch dir outside the source checkout.

Also, the comment's stated reason is inaccurate: the separate tree is not what makes the test "run on a cache hit." On a cache hit LLAR creates a fresh ctx.SourceDir and runs onTest against the persisted ctx.outputDir, so onBuild's _build never exists there regardless. A build tree separate from _build is the right convention, but its real purpose is avoiding collision with onBuild's tree during a cache-miss build-then-test run — not enabling cache-hit tests. Suggest rewording to attribute it to that (or to convention).

// the test also runs on a cache hit, where onBuild is skipped.
testDir := ctx.SourceDir + "/_llar_consumer"
testBuild := testDir + "/_build"

// The consumer mirrors the Conan test_package: include <json-c/json.h>,
// link against json-c via its installed CMake package, then build a small
// JSON object to confirm the installed headers and library are usable.
source := `#include <json-c/json.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
json_object *jobj = json_object_new_object();
json_object *jarray = json_object_new_array();
json_object_array_add(jarray, json_object_new_string("c"));
json_object_array_add(jarray, json_object_new_string("c++"));
json_object_object_add(jobj, "Categories", jarray);
printf("%s\n", json_object_to_json_string(jobj));
json_object_put(jobj);
return EXIT_SUCCESS;
}
`
os.mkdirAll(testDir, 0755)!
os.writeFile(testDir+"/consumer.c", []byte(source), 0644)!

cmakeLists := `cmake_minimum_required(VERSION 3.15)
project(json_c_consumer LANGUAGES C)
find_package(json-c REQUIRED CONFIG)
add_executable(consumer consumer.c)
target_link_libraries(consumer PRIVATE json-c::json-c)
`
os.writeFile(testDir+"/CMakeLists.txt", []byte(cmakeLists), 0644)!

tc := cmake.new(testDir, testBuild, "")
tc.use installDir
tc.configure
tc.build

exec testBuild + "/consumer"
if lastErr != nil {
panic lastErr
}
}
4 changes: 4 additions & 0 deletions json-c/json-c/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"path": "json-c/json-c",
"deps": {}
}
Loading