haxscramper / hcparse

High-level nim bindings for parsing C/C++ code
https://haxscramper.github.io/hcparse-doc/src/hcparse/libclang.html
Apache License 2.0
37 stars 2 forks source link

Stable API for wrapper generators #17

Open haxscramper opened 2 years ago

haxscramper commented 2 years ago

Stable documented API for automatically writing wrapper generators scripts using multiple parsing frontends and code expansion logic. Ideally it should not be necessary in large number of cases (for simpler libraries), but something like this libgit wrappers

let resultGrouped = wrapCSharedLibViaTsWave(
  inDir       = AbsDir"/usr/include/git2",
  outDir      = currentAbsSourceDir(),
  tmpDir      = getAppTempDir() / "v4",
  libName     = "git2",
  packageName = package,
  ignoreIn    = @["stdint"],
  depDirs     = @[ssh2],
  extraTypes  = @{
    cxxName("git_iterator"): cxxLibImport(package, @["libgit2_config"])
  }
)

or this libssh2 wrappers

let
  dir     = AbsDir"/usr/include"
  tmpDir  = getAppTempDir() / "libssh2"
  package = "hlibssh2"
  files   = @[
    dir /. "libssh2.h",
    dir /. "libssh2_sftp.h",
    dir /. "libssh2_publickey.h"
  ]
  map     = expandViaWave(files, tmpDir, baseCParseConf)
  conf    = initCSharedLibFixConf("ssh2", package, false, dir, map)
  wrapped = tmpDir.wrapViaTs(conf)
  outDir  = currentAbsSourceDir()
  grouped = writeFiles(outDir, wrapped, cCodegenConf, extraTypes = @{
    cxxName("_LIBSSH2_SESSION"): cxxLibImport(package, @["libssh2_config"])
  })

is necessary. This API can further be expanded on, to automate handling of the conan packages, git and github repositories, automatic tracking of changes (#9) and so on.

haxscramper commented 2 years ago

Need to add support for interfacing with C++ package managers, at least with conan. Tried using it in CI builds - absolutely awesome, amounts to several very simple commands and one (not really obvious though) flag passed to the gcc - @$(pwd)/conanbuildinfo.gcc allows reading list of flags from the conanbuildinfo.gcc file generated by conan.

pip install conan
conan install . --build=missing
nimble -y develop
nim c -r \
      --passL:"@$(pwd)/conanbuildinfo.gcc" \
      --passC:"@$(pwd)/conanbuildinfo.gcc" \
      -d:libgit2LinkMode=dlink \
      -d:libssh2LinkMode=dlink \
      tests/test1.nim
haxscramper commented 2 years ago

In addition to the API wrapper generation, it is also necessary to properly reuse all the type import maps from the dependencies. For example, libssh2 contains a file with all the types encountered when wrapping this library. When libgit wrapper is generated, it must know a list of dependencies to be able to properly read import maps. In an ideal world, this could've been solved by asking package manager to list the locations of all the dependencies, and then simply searching for the hcparse_type_imports.xml file in the repositories. Nimble cannot do this at all, but with nimph I can at least try and read the environment in which wrapper generation is performed, and find all target import maps.