export c header file for build library

This commit is contained in:
Li Jie
2025-09-11 14:07:58 +08:00
parent 729e5772a0
commit 3605eeeef7
5 changed files with 1465 additions and 45 deletions

View File

@@ -43,6 +43,7 @@ import (
"github.com/goplus/llgo/internal/env"
"github.com/goplus/llgo/internal/firmware"
"github.com/goplus/llgo/internal/flash"
"github.com/goplus/llgo/internal/header"
"github.com/goplus/llgo/internal/mockable"
"github.com/goplus/llgo/internal/monitor"
"github.com/goplus/llgo/internal/packages"
@@ -375,7 +376,15 @@ func Do(args []string, conf *Config) ([]Package, error) {
// Generate C headers for c-archive and c-shared modes before linking
if ctx.buildConf.BuildMode == BuildModeCArchive || ctx.buildConf.BuildMode == BuildModeCShared {
headerErr := generateCHeader(ctx, pkg, outFmts.Out, verbose)
libname := strings.TrimSuffix(filepath.Base(outFmts.Out), conf.AppExt)
headerPath := filepath.Join(filepath.Dir(outFmts.Out), libname) + ".h"
pkgs := make([]llssa.Package, 0, len(allPkgs))
for _, p := range allPkgs {
if p.LPkg != nil {
pkgs = append(pkgs, p.LPkg)
}
}
headerErr := header.GenHeaderFile(prog, pkgs, libname, headerPath, verbose)
if headerErr != nil {
return nil, headerErr
}
@@ -818,44 +827,6 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, global l
return nil
}
// TODO(lijie): export C header from function list of the pkg
func generateCHeader(ctx *context, pkg *packages.Package, outputPath string, verbose bool) error {
// Determine header file path
headerPath := strings.TrimSuffix(outputPath, filepath.Ext(outputPath)) + ".h"
// Generate header content
headerContent := fmt.Sprintf(`/* Code generated by llgo; DO NOT EDIT. */
#ifndef __%s_H_
#define __%s_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* __%s_H_ */
`,
strings.ToUpper(strings.ReplaceAll(pkg.Name, "-", "_")),
strings.ToUpper(strings.ReplaceAll(pkg.Name, "-", "_")),
strings.ToUpper(strings.ReplaceAll(pkg.Name, "-", "_")))
// Write header file
err := os.WriteFile(headerPath, []byte(headerContent), 0644)
if err != nil {
return fmt.Errorf("failed to write header file %s: %w", headerPath, err)
}
if verbose {
fmt.Fprintf(os.Stderr, "Generated C header: %s\n", headerPath)
}
return nil
}
func linkObjFiles(ctx *context, app string, objFiles, linkArgs []string, verbose bool) error {
// Handle c-archive mode differently - use ar tool instead of linker
if ctx.buildConf.BuildMode == BuildModeCArchive {