Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waf generator and some tests #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
742 changes: 742 additions & 0 deletions extensions/generators/Waf.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tests/waf/test_basic/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os, json
from conan import ConanFile
from conan.tools.files import copy

class WafConanTestProject(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = "spdlog/1.12.0"

generators = ['Waf']
# python_requires = "wafgenerator/0.1"
# def generate(self):
# gen = self.python_requires["wafgenerator"].module.Waf(self)
# gen.generate()
6 changes: 6 additions & 0 deletions tests/waf/test_basic/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <spdlog/spdlog.h>

int main(int argc, char *argv[]){
spdlog::info("Hello, waf! {}", argv[0]);
return 0;
}
41 changes: 41 additions & 0 deletions tests/waf/test_basic/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import shutil
import tempfile
import os, sys

import pytest

from tools import run

@pytest.fixture(autouse=True)
def conan_test():
old_env = dict(os.environ)
env_vars = {"CONAN_HOME": tempfile.mkdtemp(suffix='conans')}
os.environ.update(env_vars)
current = tempfile.mkdtemp(suffix="conans")
cwd = os.getcwd()
os.chdir(current)
try:
yield
finally:
os.chdir(cwd)
os.environ.clear()
os.environ.update(old_env)

def test_waf_basic():
repo = os.path.join(os.path.dirname(__file__), "../../..")
run(f"conan config install {repo}")
run("conan --help")
run("conan profile detect")

os.chdir(os.path.dirname(__file__))
run(f"{sys.executable} ../waf distclean")

run("conan install . -of=build --build=missing")
run(f"{sys.executable} ../waf configure build -v")
dir_list = os.listdir('build')
if 'app' in dir_list:
run(os.path.join('build', 'app'))
elif 'app.exe' in dir_list:
run(os.path.join('build', 'app.exe'))
else:
assert 0, 'missing output'
14 changes: 14 additions & 0 deletions tests/waf/test_basic/wscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def options(opt):
opt.load('compiler_cxx')

def configure(conf):
conf.load('conan_deps', tooldir='build')
conf.load('compiler_cxx')

def build(bld):
bld(
features = "cxx cxxprogram conan",
source = "main.cpp",
use = "spdlog",
target = "app"
)
14 changes: 14 additions & 0 deletions tests/waf/test_flatbuffers/car.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Test;

table Manufacturer {
name:string;
coolness:uint8;
}

table Car {
make:Manufacturer;
model:string;
year:uint16;
}

root_type Car;
19 changes: 19 additions & 0 deletions tests/waf/test_flatbuffers/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os, json
from conan import ConanFile
from conan.tools.files import copy

class WafConanTestProjectFlatbuffers(ConanFile):
name = "waf_conan_test_flatbuffers"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"

requires = "flatbuffers/23.5.26"

#ensure 'flatc' is available in build environment
tool_requires = "flatbuffers/23.5.26"

generators = ['Waf']
# python_requires = "wafgenerator/0.1"
# def generate(self):
# gen = self.python_requires["wafgenerator"].module.Waf(self)
# gen.generate()
56 changes: 56 additions & 0 deletions tests/waf/test_flatbuffers/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "car_generated.h"
#include <flatbuffers/flatbuffers.h>
#include <cinttypes>
#include <fstream>
#include <iostream>

using namespace Test;

void read_buf(const char *filename){
std::ifstream file(filename, std::ios_base::binary);
file.seekg(0, std::ios_base::end);
size_t length = file.tellg();
void *data = malloc(length);
file.seekg(0);
file.read((char*)data, length);
auto car = GetCar(data);
std::cout << "Model: " << car->model()->c_str() << std::endl;
std::cout << "Year: " << car->year() << std::endl;
std::cout << "Make: " << car->make()->name()->c_str();
std::cout << " (" << (int)car->make()->coolness() << ")" << std::endl;
file.close();
free(data);
}

void write_buf(
const char *filename,
const char *make,
const char *model,
uint16_t year,
uint8_t coolness
){
flatbuffers::FlatBufferBuilder builder(1024);
auto mfname = builder.CreateString(make);
auto mf = CreateManufacturer(builder, mfname, coolness);
auto m = builder.CreateString(model);
auto car = CreateCar(builder, mf, m, year);
builder.Finish(car);

uint8_t *buf = builder.GetBufferPointer();
int size = builder.GetSize();
std::ofstream file(filename, std::ios_base::binary);
file.write((char*)buf, size);
file.close();
}

int main(int argc, char *argv[]){
//write
write_buf("car1.bin", "McCar", "Nugget", 2033, 22);
write_buf("car2.bin", "Car King", "Flopper", 2032, 43);

//read
read_buf("car1.bin");
std::cout << "-----" << std::endl;
read_buf("car2.bin");
return 0;
}
41 changes: 41 additions & 0 deletions tests/waf/test_flatbuffers/test_flatbuffers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import shutil
import tempfile
import os, sys

import pytest

from tools import run

@pytest.fixture(autouse=True)
def conan_test():
old_env = dict(os.environ)
env_vars = {"CONAN_HOME": tempfile.mkdtemp(suffix='conans')}
os.environ.update(env_vars)
current = tempfile.mkdtemp(suffix="conans")
cwd = os.getcwd()
os.chdir(current)
try:
yield
finally:
os.chdir(cwd)
os.environ.clear()
os.environ.update(old_env)

def test_waf_flatbuffers():
repo = os.path.join(os.path.dirname(__file__), "../../..")
run(f"conan config install {repo}")
run("conan --help")
run("conan profile detect")

os.chdir(os.path.dirname(__file__))
run(f"{sys.executable} ../waf distclean")

run("conan install . -of=build --build=missing")
run(f"{sys.executable} ../waf configure build -v")
dir_list = os.listdir('build')
if 'app' in dir_list:
run(os.path.join('build', 'app'))
elif 'app.exe' in dir_list:
run(os.path.join('build', 'app.exe'))
else:
assert 0, 'missing output'
33 changes: 33 additions & 0 deletions tests/waf/test_flatbuffers/wscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def options(opt):
opt.load('compiler_cxx')

def configure(conf):
conf.load('conan_deps', tooldir='build')
conf.load('compiler_cxx')

#flatc will be found in conan cache
conf.find_program("flatc", var="FLATC")

def build(bld):
#declare the expected output from flatc
gen_header = bld.path.get_bld().find_or_declare("generated/car_generated.h")

#generate headers using our schema
bld(
rule = "${FLATC} --cpp -o ${TGT[0].parent} ${SRC}",
source = "car.fbs",
target = gen_header
)

#compile application
bld(
features = "cxx cxxprogram conan",
source = "main.cpp",
includes = gen_header.parent.abspath(),
use = "flatbuffers",
target = "app"
)

#waf's include scanner will detect that 'main.cpp' is importing a file
#generated by flatc (aka the `target` keyword above), and will ensure
#proper execution order
4 changes: 4 additions & 0 deletions tests/waf/test_ndk_cross/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```sh
conan install . -of=build --build=missing -pr:h=./android_arm64.profile -pr:b=default
../waf configure build
```
11 changes: 11 additions & 0 deletions tests/waf/test_ndk_cross/android_arm64.profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[settings]
arch=armv8
build_type=Release
compiler=clang
compiler.libcxx=c++_static
compiler.version=17
os=Android
os.api_level=30

[tool_requires]
*: android-ndk/r26b
14 changes: 14 additions & 0 deletions tests/waf/test_ndk_cross/car.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Test;

table Manufacturer {
name:string;
coolness:uint8;
}

table Car {
make:Manufacturer;
model:string;
year:uint16;
}

root_type Car;
14 changes: 14 additions & 0 deletions tests/waf/test_ndk_cross/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os, json
from conan import ConanFile
from conan.tools.files import copy

class WafConanTestProjectNDK(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = ["flatbuffers/23.5.26"]
build_requires = ["flatbuffers/23.5.26"]

generators = ['Waf']
# python_requires = "wafgenerator/0.1"
# def generate(self):
# gen = self.python_requires["wafgenerator"].module.Waf(self)
# gen.generate()
45 changes: 45 additions & 0 deletions tests/waf/test_ndk_cross/mylib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "car_generated.h"
#include <flatbuffers/flatbuffers.h>
#include <cinttypes>
#include <fstream>
#include <android/log.h>

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "mylib", __VA_ARGS__))

using namespace Test;

void read_car(const char *filename){
std::ifstream file(filename, std::ios_base::binary);
file.seekg(0, std::ios_base::end);
size_t length = file.tellg();
void *data = malloc(length);
file.seekg(0);
file.read((char*)data, length);
auto car = GetCar(data);
LOGI("Model: (%s)", car->model()->c_str());
LOGI("Year: (%d)", (int)car->year());
LOGI("Make: %s (%d)", car->make()->name()->c_str(), (int)car->make()->coolness());
file.close();
free(data);
}

void write_car(
const char *filename,
const char *make,
const char *model,
uint16_t year,
uint8_t coolness
){
flatbuffers::FlatBufferBuilder builder(1024);
auto mfname = builder.CreateString(make);
auto mf = CreateManufacturer(builder, mfname, coolness);
auto m = builder.CreateString(model);
auto car = CreateCar(builder, mf, m, year);
builder.Finish(car);

uint8_t *buf = builder.GetBufferPointer();
int size = builder.GetSize();
std::ofstream file(filename, std::ios_base::binary);
file.write((char*)buf, size);
file.close();
}
35 changes: 35 additions & 0 deletions tests/waf/test_ndk_cross/test_ndk_cross.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import shutil
import tempfile
import os, sys

import pytest

from tools import run

@pytest.fixture(autouse=True)
def conan_test():
old_env = dict(os.environ)
env_vars = {"CONAN_HOME": tempfile.mkdtemp(suffix='conans')}
os.environ.update(env_vars)
current = tempfile.mkdtemp(suffix="conans")
cwd = os.getcwd()
os.chdir(current)
try:
yield
finally:
os.chdir(cwd)
os.environ.clear()
os.environ.update(old_env)

def test_waf_ndk_cross():
repo = os.path.join(os.path.dirname(__file__), "../../..")
run(f"conan config install {repo}")
run("conan --help")
run("conan profile detect")

os.chdir(os.path.dirname(__file__))
run(f"{sys.executable} ../waf distclean")

run("conan install . -of=build -pr:h=./android_arm64.profile -pr:b=default --build=missing")
run(f"{sys.executable} ../waf configure build -v")
# TODO: check if output lib matches profile arch
Loading