Skip to content

Commit

Permalink
(#16533) lemon: conan v2 support
Browse files Browse the repository at this point in the history
* conan v2 support

* remove useless CMakeDeps in test package
  • Loading branch information
SpaceIm authored Mar 28, 2023
1 parent 1ff49ae commit af1e026
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 59 deletions.
13 changes: 6 additions & 7 deletions recipes/lemon/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cmake_minimum_required(VERSION 2.8.14)
project(ConanLemon C)
cmake_minimum_required(VERSION 3.15)
project(Lemon LANGUAGES C)

include(conanbuildinfo.cmake)
conan_basic_setup()
add_executable(lemon ${LEMON_SRC_DIR}/tool/lemon.c)

add_executable(lemon source_subfolder/tool/lemon.c)
install(TARGETS lemon DESTINATION bin)
install(FILES source_subfolder/tool/lempar.c DESTINATION bin)
include(GNUInstallDirs)
install(TARGETS lemon DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES ${LEMON_SRC_DIR}/tool/lempar.c DESTINATION ${CMAKE_INSTALL_BINDIR})
5 changes: 2 additions & 3 deletions recipes/lemon/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
sources:
"3.32.3":
"url": "https://sqlite.org/2020/sqlite-src-3320300.zip"
"sha256": "9312f0865d3692384d466048f746d18f88e7ffd1758b77d4f07904e03ed5f5b9"
url: "https://sqlite.org/2020/sqlite-src-3320300.zip"
sha256: "9312f0865d3692384d466048f746d18f88e7ffd1758b77d4f07904e03ed5f5b9"
patches:
"3.32.3":
- patch_file: "patches/0001-use-executable-template-path.patch"
base_path: "source_subfolder"
61 changes: 31 additions & 30 deletions recipes/lemon/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
from conans import CMake, ConanFile, tools
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, load, save
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.53.0"


class LemonConan(ConanFile):
name = "lemon"
description = "The Lemon program reads a grammar of the input language and emits C-code to implement a parser for that language."
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://sqlite.org/lemon.html"
topics = ("conan", "lemon", "grammar", "lexer", "lalr", "parser", "generator", "sqlite")
topics = ("grammar", "lexer", "lalr", "parser", "generator", "sqlite")
license = "Unlicense"
exports_sources = "CMakeLists.txt", "patches/**"
settings = "os", "compiler", "arch", "build_type"
generators = "cmake"
package_type = "application"
settings = "os", "arch", "compiler", "build_type"

@property
def _source_subfolder(self):
return "source_subfolder"

_cmake = None
def export_sources(self):
copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder)
export_conandata_patches(self)

def configure(self):
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def layout(self):
cmake_layout(self, src_folder="src")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.configure()
return self._cmake
def generate(self):
tc = CMakeToolchain(self)
tc.variables["LEMON_SRC_DIR"] = self.source_folder.replace("\\", "/")
tc.generate()

def build(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
cmake = self._configure_cmake()
apply_conandata_patches(self)
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
cmake.build()

def _extract_license_text(self):
header = tools.load(os.path.join(self._source_subfolder, "tool", "lempar.c"))
header = load(self, os.path.join(self.source_folder, "tool", "lempar.c"))
return "\n".join(line.strip(" \n*") for line in header[3:header.find("*******", 1)].splitlines())

def package(self):
tools.save(os.path.join(self.package_folder, "licenses", "LICENSE"), self._extract_license_text())
cmake = self._configure_cmake()
save(self, os.path.join(self.package_folder, "licenses", "LICENSE"), self._extract_license_text())
cmake = CMake(self)
cmake.install()

def package_info(self):
bin_path = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bin_path))
self.env_info.PATH.append(bin_path)
self.cpp_info.includedirs = []
self.cpp_info.libdirs = []

# TODO: to remove in conan v2
self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))
13 changes: 6 additions & 7 deletions recipes/lemon/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)
cmake_minimum_required(VERSION 3.2)
project(test_package LANGUAGES C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_program(LEMON_PROGRAM NAMES lemon)

add_custom_command(OUTPUT gram.c gram.h
COMMAND lemon -s "${PROJECT_SOURCE_DIR}/gram.y" -d"${PROJECT_BINARY_DIR}"
add_custom_command(
OUTPUT gram.c gram.h
COMMAND ${LEMON_PROGRAM} -s "${PROJECT_SOURCE_DIR}/gram.y" -d"${PROJECT_BINARY_DIR}"
BYPRODUCTS gram.out
)

add_executable(${PROJECT_NAME} gram.c)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
28 changes: 18 additions & 10 deletions recipes/lemon/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeToolchain", "VirtualBuildEnv", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def build_requirements(self):
self.tool_requires(self.tested_reference_str)

def build(self):
if not tools.cross_building(self.settings):
cmake = CMake(self)
cmake.configure()
cmake.build()
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/lemon/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)
21 changes: 21 additions & 0 deletions recipes/lemon/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
test_type = "explicit"

def build_requirements(self):
self.build_requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
4 changes: 2 additions & 2 deletions recipes/lemon/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"versions":
versions:
"3.32.3":
"folder": "all"
folder: "all"

0 comments on commit af1e026

Please sign in to comment.