Skip to content

Instantly share code, notes, and snippets.

@grisumbras
Created May 22, 2024 15:21
Show Gist options
  • Select an option

  • Save grisumbras/040b3b5ffc3572e395cbb3a8c9c11c09 to your computer and use it in GitHub Desktop.

Select an option

Save grisumbras/040b3b5ffc3572e395cbb3a8c9c11c09 to your computer and use it in GitHub Desktop.
B2 lib with internal tool
import host-platform ;
import mox ;
project myproj ;
exe moxtool : moxtool.cpp ;
explicit moxtool ;
hpp foobar.hpp : foobar.mox ;
lib mylib : foobar.hpp xyz.cpp ;
import "class" : new ;
import generators ;
import path ;
import property ;
import property-set ;
import type ;
type.register MOX : mox ;
class mox-to-cpp-generator : generator
{
import targets ;
import host-platform ;
rule run ( project name ? : ps : source )
{
if ! $(name)
{
name = [ generator.determine-output-name $(source) ] ;
}
local host-ps = [ host-property-set $(ps) ] ;
local moxtool = [ targets.generate-from-reference /myproj//moxtool
: $(project) : $(host-ps) ] ;
moxtool = $(moxtool[2]) ;
local result =
[ generator.run $(project) $(name)
: [ property-set.create <moxtool>$(moxtool) ]
: $(source)
] ;
$(result[2]).depends $(moxtool) ;
local include-dir = [ $(result[2]).path ] ;
return [ $(result[1]).add-raw <include>$(include-dir) ] $(result[2-]) ;
}
local rule host-property-set ( ps )
{
local host-ps = [ $(ps).get <host-platform> ] ;
if $(host-ps)
{
host-ps = [ host-platform.get $(host-ps:G=) ] ;
echo F $(host-ps) ;
host-ps =
[ property.refine [ $(ps).raw ]
: [ feature.expand-composites $(host-ps) ]
] ;
ps = [ property-set.create $(host-ps) ] ;
}
return $(ps) ;
}
}
generators.register [ new mox-to-cpp-generator mox.to-hpp : MOX : HPP ] ;
rule to-hpp ( target : sources * : props * )
{
local moxtool = [ property.select <moxtool> : $(props) ] ;
moxtool = $(moxtool:G=) ;
MOXTOOL on $(target) =
[ path.native [ path.root [ $(moxtool).name ] [ $(moxtool).path ] ] ] ;
}
actions to-hpp
{
$(MOXTOOL) $(>[1]) $(>[2]) $(<)
}
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char const** argv)
{
if (argc < 3) { return EXIT_FAILURE; }
std::ifstream is;
is.exceptions(std::ios::failbit | std::ios::badbit);
is.open(argv[1]);
std::string name;
is >> name;
is.close();
std::ofstream os;
os.exceptions(std::ios::failbit | std::ios::badbit);
os.open(argv[2], std::ios::out | std::ios::trunc);
os << "inline\n"
<< "void " << name << "()\n"
<< "{}\n";
os.close();
}
import host-platform ;
host-platform.add
x86_64-linux-gnu-release
: <architecture>x86
<address-model>64
<target-os>linux
<variant>release
;
#include "foobar.hpp"
void xyz() { foobar(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment