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

Add parser for pkg-config files #94

Merged
merged 4 commits into from
Feb 11, 2025
Merged
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
5 changes: 3 additions & 2 deletions .github/workflows/meson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Brew Install
run: brew install pkg-config ninja cli11 googletest nlohmann-json fmt cmake meson tl-expected
run: brew install pkg-config ninja cli11 googletest nlohmann-json fmt cmake meson tl-expected bison flex
- name: Build cps-config
run: |
meson setup builddir -Dunity=on -Dunity_size=12
PATH=/opt/homebrew/opt/bison/bin:/opt/homebrew/opt/flex/bin:$PATH \
meson setup builddir -Dunity=on -Dunity_size=12
ninja -C builddir
- name: Run tests
run: meson test -C builddir
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# PC file parser
find_package(BISON REQUIRED)
find_package(FLEX REQUIRED)
bison_target(PcParser cps/pc_compat/pc.y ${CMAKE_CURRENT_BINARY_DIR}/cps/pc_compat/pc.parser.cpp)
flex_target(PcScanner cps/pc_compat/pc.l ${CMAKE_CURRENT_BINARY_DIR}/cps/pc_compat/pc.lexer.cpp)
add_flex_bison_dependency(PcScanner PcParser)

# cps library
add_library(
cps
Expand All @@ -8,6 +15,9 @@ add_library(
cps/search.cpp
cps/utils.cpp
cps/version.cpp
${BISON_PcParser_OUTPUTS}
${FLEX_PcScanner_OUTPUTS}
Comment on lines +18 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh

It seems probable that this is how the CMake modules for Bison and Flex were designed, but for the record, it's gross to expand strings into add_library calls instead of having a target_bison_codegen function that just adds the source files to your target for you. The function should also be adding the needed include directory for you so you don't have to add "magic" include paths as you do below.

Let's circle back with Kitware about whether the flex and bison codegen need another look, especially now that CMake 3.31 will support code generation better than before.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to close the feedback loop, this is still something that needs cleaned up if someone's looking for a good upstream CMake contribution to start with. But it wasn't scoped into any work yet. Using the API like this is the best thing we can do for now.

cps/pc_compat/pc_loader.cpp
)

# Configure config.hpp
Expand Down
3 changes: 3 additions & 0 deletions src/cps/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace cps {
// TODO: Windows
env.cps_prefix_path = utils::split(env_c, ":");
}
if (const char * env_c = std::getenv("PKG_CONFIG_PATH")) {
env.pc_path = std::string(env_c);
}
if (std::getenv("PKG_CONFIG_DEBUG_SPEW") || std::getenv("CPS_CONFIG_DEBUG_SPEW")) {
env.debug_spew = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/cps/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace cps {
struct Env {
std::optional<std::string> cps_path = std::nullopt;
std::optional<std::vector<std::string>> cps_prefix_path = std::nullopt;
std::optional<std::string> pc_path = std::nullopt;
bool debug_spew = false;
};

Expand Down
2 changes: 0 additions & 2 deletions src/cps/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace cps::loader {

namespace {

constexpr static std::string_view CPS_VERSION = "0.12.0";

template <typename T>
tl::expected<std::optional<T>, std::string>
get_optional(const nlohmann::json & parent, std::string_view parent_name, const std::string & name) {
Expand Down
2 changes: 2 additions & 0 deletions src/cps/loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ namespace cps::loader {
version::Schema version_schema;
};

constexpr inline std::string_view CPS_VERSION = "0.12.0";

tl::expected<Package, std::string> load(std::istream & input_buffer, const std::filesystem::path & filename);

} // namespace cps::loader
2 changes: 2 additions & 0 deletions src/cps/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: MIT
# Copyright © 2023-2024 Dylan Baker

subdir('pc_compat')

conf = configuration_data()
conf.set_quoted('CPS_CONFIG_VERSION', meson.project_version())
conf.set_quoted('CPS_CONFIG_LIBDIR', get_option('libdir'))
Expand Down
34 changes: 34 additions & 0 deletions src/cps/pc_compat/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
_flex_version = '>= 2.6'
lunacd marked this conversation as resolved.
Show resolved Hide resolved
_bison_version = '>= 2.6'
prog_flex = find_program('', required : false)
flex_args = []
prog_bison = find_program('', required : false)
if host_machine.system() == 'windows'
prog_flex = find_program('win_flex', required : false, version : _flex_vesrion)
if prog_flex.found()
# This uses <io.h> instead of <unistd.h>
flex_args = ['--wincompat']
endif

prog_bison = find_program('win_bison', required : false, version : _bison_version)
endif
if not prog_flex.found()
prog_flex = find_program('flex', version : _flex_version)
endif
if not prog_bison.found()
prog_bison = find_program('bison', version : _bison_version)
endif

pc_scanner = custom_target(
'pc_scanner',
command : [prog_flex, flex_args, '--outfile=@OUTPUT0@', '--header-file=@OUTPUT1@', '@INPUT@'],
input : 'pc.l',
output : ['@[email protected]', '@[email protected]'],
)

pc_parser = custom_target(
'pc_parser',
command : [prog_bison, '-d', '@INPUT@', '-v', '--output=@OUTPUT0@', '--defines=@OUTPUT1@'],
input : 'pc.y',
output : ['@[email protected]', '@[email protected]', 'locations.hpp']
)
lunacd marked this conversation as resolved.
Show resolved Hide resolved
63 changes: 63 additions & 0 deletions src/cps/pc_compat/pc.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* SPDX-License-Identifier: MIT
* Copyright © 2024 Haowen Liu
*/

%option noyywrap

%{
#include <string>
#include <istream>
#include "cps/pc_compat/pc_loader.hpp"
#include "cps/pc_compat/pc.parser.hpp"
%}

/* To debug scanner, set debug as an option */
/* TODO: Add a way to debug scanner without rebuilding */
%option noyywrap nounput noinput batch

%{
std::istream *yyinput = nullptr;

#define YY_INPUT(buf,result,max_size) { \
yyinput->read(buf, max_size); \
result = yyinput->gcount(); \
}
%}

str [^ \t\r\n#:=${}]+
blank [ \t\r]+
comment #[^\n]*

%%
":" { return yy::parser::make_COLON(); }
"=" { return yy::parser::make_EQ(); }
"<" { return yy::parser::make_LT(); }
"<=" { return yy::parser::make_LE(); }
"!=" { return yy::parser::make_NE(); }
">=" { return yy::parser::make_GE(); }
">" { return yy::parser::make_GT(); }
"\n" { return yy::parser::make_LF(); }
"$" { return yy::parser::make_DOLLAR(); }
"{" { return yy::parser::make_LBRACE(); }
"}" { return yy::parser::make_RBRACE(); }
"," { return yy::parser::make_COMMA(); }
"Requires" { return yy::parser::make_REQUIRES(); }
"Requires.private" { return yy::parser::make_REQUIRES_P(); }
"Conflicts" { return yy::parser::make_CONFLICTS(); }
"Provides" { return yy::parser::make_PROVIDES(); }

{comment} {}
{blank} { return yy::parser::make_BLANK(yytext); }
{str} { return yy::parser::make_STR(yytext); }
<<EOF>> { return yy::parser::make_YYEOF(); }
%%

namespace cps::pc_compat {

void PcLoader::scan_begin(std::istream &istream) const {
yyinput = &istream;
}

}

173 changes: 173 additions & 0 deletions src/cps/pc_compat/pc.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// SPDX-License-Identifier: MIT

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an existing BNF grammar for pkg config files we can refer to or quote?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of. But there probably is? I was using https://manpages.ubuntu.com/manpages/jammy/man5/pc.5.html as my reference

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see one either, and pkg-config uses a hand-rolled recursive descent parser.
https://gitlab.freedesktop.org/pkg-config/pkg-config/-/blob/master/parse.c?ref_type=heads#L908

One thing I note from that is CFlags can also be spelled Cflags. 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkgconf also uses a hand rolled recursive descent parser, but that man page is from pkgconf. My preference would be to emulate pkgconf rather than pkg-config if possible, since it actually has developers, and most distros are shipping pkgconf with a symlink to pkg-config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I'm fine with landing in the current form, and if we encounter compatibility issues then we can make tweaks as needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a branch of pkgconf (that probably needs some additional work) that ports the kyua tests to a python runner. I can try to get those around for cps-config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. When you do, feel free to ping me to fix any issues (I bet there would be a lot)

// Copyright © 2024 Haowen Liu

%skeleton "lalr1.cc"

%define api.token.raw

%define api.token.constructor
%define api.value.type variant
%define parse.assert

%code requires {
#include "cps/utils.hpp"
#include "cps/pc_compat/pc_base.hpp"
}

// The parsing context.
%param { cps::pc_compat::PcLoader& loader }

%define parse.trace
%define parse.error detailed
%define parse.lac full

%code {
#include "cps/pc_compat/pc_loader.hpp"
}

%define api.token.prefix {TOK_}
%token
COLON ":"
LF "\n"
EQ "="
LT "<"
LE "<="
NE "!="
GT ">"
GE ">="
DOLLAR "$"
LBRACE "{"
RBRACE "}"
COMMA ","
REQUIRES "Requires"
REQUIRES_P "Requires.private"
CONFLICTS "Conflicts"
PROVIDES "Provides"
;

%token <std::string> STR "str"
%token <std::string> BLANK "blank"
%nterm <std::string> literal
%nterm <std::string> variable
%nterm <std::string> name
%nterm <cps::pc_compat::VersionOperation> version_op_token
%nterm <cps::pc_compat::VersionOperation> version_op
%nterm <cps::pc_compat::PackageRequirement> package_requirement
%nterm <std::vector<cps::pc_compat::PackageRequirement>> package_requirements
%nterm <std::variant<std::string, std::vector<cps::pc_compat::PackageRequirement>>> literal_property

%printer { yyo << $$; } <*>;

%%
%start file;

// This rule allows for the last line to not be terminated by '\n'
file:
lines
| lines statement;

lines:
%empty
| lines line;

// In this grammar, leading whitespace is handled by this rule.
// Everything else only takes care of trailing whitespace.
line:
"\n"
| statement "\n"
| "blank" statement "\n";

// Statement is a meaningful line, not including line feed
statement:
property
| assignment;

// property is a line that sets a property to a value
property:
"Requires" colon package_requirements { loader.properties.emplace("Requires", $3); }
| "Requires.private" colon package_requirements { loader.properties.emplace("Requires.private", $3); }
| "Conflicts" colon package_requirements { loader.properties.emplace("Conflicts", $3); }
| "Provides" colon package_requirements { loader.properties.emplace("Provides", $3); }
| name colon literal_property { loader.properties.emplace($1, $3); }

// version_op_token captures all the valid tokens for version comparison
version_op_token:
"<" { $$ = cps::pc_compat::VersionOperation::lt; }
| "<=" { $$ = cps::pc_compat::VersionOperation::le; }
| "=" { $$ = cps::pc_compat::VersionOperation::eq; }
| "!=" { $$ = cps::pc_compat::VersionOperation::ne; }
| ">" { $$ = cps::pc_compat::VersionOperation::gt; }
| ">=" { $$ = cps::pc_compat::VersionOperation::ge; };

// version_op handles trailing space for version comparisons
version_op:
version_op_token
| version_op_token "blank" { $$ = $1; };

// package_requirement parses a package name, optionally followed by some version requirement
package_requirement:
name version_op "str" {
$$ = cps::pc_compat::PackageRequirement {
.package = $1,
.operation = $2,
.version = $3,
};
}
| package_requirement "blank" { $$ = $1; };

// package_requirements is a comma separated list of package_requirement
package_requirements:
package_requirement { $$ = std::vector{$1}; }
| package_requirements comma package_requirement {
$1.emplace_back($3);
$$ = $1;
}
| package_requirements comma "str" {
$1.emplace_back(cps::pc_compat::PackageRequirement {
.package = $3,
.operation = std::nullopt,
.version = std::nullopt,
});
$$ = $1;
};

// assignment is a line that sets a variable to a value
assignment:
name "=" literal { loader.variables.emplace($1, cps::utils::trim($3)); };

// name handles surrounding spaces for a variable or property name
name:
"str"
| "str" "blank" { $$ = $1; };

// literal_property constructs a variant with the trimmed literal value
literal_property:
literal { $$ = cps::pc_compat::PcPropertyValue{std::in_place_type<std::string>, cps::utils::trim($1)}; };

// Literal is a literal string. This could contain trailing whitespace so trim the result before using.
literal:
":" { $$ = ":"; }
| "str" { $$ = $1; }
| variable { $$ = $1; }
| literal ":" { $$ = $1 + ":"; }
| literal "str" { $$ = $1 + $2; }
| literal variable { $$ = $1 + $2; }
| literal "blank" { $$ = $1 + $2; };

variable:
"$" "{" "str" "}" { $$ = loader.variables[$3]; }

// colon and comma handles trailing whitespace
colon:
":"
| ":" "blank";
comma:
","
| "," "blank";
%%

void
yy::parser::error (const std::string& m)
{
std::cerr << "Error: " << m << '\n';
}
37 changes: 37 additions & 0 deletions src/cps/pc_compat/pc_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
// Copyright © 2024 Haowen Liu

// This header is created to avoid writing non-trivial C++ code in pc.y.
// This header contains types and declarations that are needed by both loader and parser.

#pragma once

#include <optional>
#include <ostream>
#include <string>
#include <variant>
#include <vector>

namespace cps::pc_compat {
class PcLoader;

// The following types needs to be declared in the parser because
// PackageRequirement is the type of a non-terminal. bison needs
// to do a sizeof on this type and cannot do so with a forward
// declaration.
enum class VersionOperation { lt, le, ne, eq, gt, ge };
struct PackageRequirement {
std::string package;
std::optional<VersionOperation> operation;
std::optional<std::string> version;

// For parser debug output
friend std::ostream & operator<<(std::ostream & ost, const PackageRequirement & package_requirement);
};

// For parser debug output
std::ostream & operator<<(std::ostream & ost, const std::optional<VersionOperation> & version_operation);
std::ostream & operator<<(std::ostream & ost, const std::vector<PackageRequirement> & package_requirements);
std::ostream & operator<<(std::ostream & ost,
const std::variant<std::string, std::vector<PackageRequirement>> & property_value);
} // namespace cps::pc_compat
Loading
Loading