Skip to content

Commit

Permalink
Add configuration handling from environment variables (#84)
Browse files Browse the repository at this point in the history
Adds environment variable handling for common configuration variables.

Based on #83.
  • Loading branch information
taalexander authored Mar 29, 2023
1 parent 970aca4 commit 07fbafa
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/Config/CLIConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ llvm::cl::OptionCategory &getQSSCCategory();
///
/// The qss-compiler adds several cli arguments to
/// configure the QSSConfig through the CLIConfigBuilder.
///
/// These currently are:
/// - `--target=<str>`: Sets QSSConfig::targetName.
/// - `--config=<str>`: Sets QSSConfig::targetConfigPath.
Expand Down
43 changes: 43 additions & 0 deletions include/Config/EnvVarConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===- EnvVarConfig.h - EnvVar Configuration builder ----------*- C++-*----===//
//
// (C) Copyright IBM 2023.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
//
//===----------------------------------------------------------------------===//
///
/// Populate the configuration from environment variables.
///
//===----------------------------------------------------------------------===//
#ifndef QSSC_ENVVARCONFIG_H
#define QSSC_ENVVARCONFIG_H

#include "Config/QSSConfig.h"

namespace qssc::config {

/// @brief Populate arguments of the QSSConfig
/// from environment variables.
///
///
/// The qss-compiler makes several several QSSConfig configuration
/// options configurable from environment variables through the
/// EnvVarConfigBuilder.
///
/// These currently are:
/// - `QSSC_TARGET_NAME`: Sets QSSConfig::targetName.
/// - `QSSC_TARGET_CONFIG_PATH`: Sets QSSConfig::targetConfigPath.
///
class EnvVarConfigBuilder : public QSSConfigBuilder {
public:
llvm::Error populateConfig(QSSConfig &config) override;

private:
llvm::Error populateConfigurationPath_(QSSConfig &config);
llvm::Error populateTarget_(QSSConfig &config);
};

} // namespace qssc::config
#endif // QSS_ENVVARCONFIG_H
20 changes: 18 additions & 2 deletions lib/API/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "API/api.h"
#include "Config/CLIConfig.h"
#include "Config/EnvVarConfig.h"

#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
Expand Down Expand Up @@ -301,16 +302,31 @@ static void printVersion(llvm::raw_ostream &out) {

/// @brief Build the QSSConfig using the standard sources and assign to the
/// supplied context.
///
/// The configuration precedence order is
/// 1. Default values
/// 2. Environment variables
/// 3. CLI arguments.
///
/// @param context The context to build and register the configuration for.
/// @return The constructed configuration that has been registered for the
/// supplied context.
static llvm::Expected<const qssc::config::QSSConfig &>
buildConfig_(mlir::MLIRContext *context) {
// Build configuration only from the CLI for now.
auto config = qssc::config::CLIConfigBuilder().buildConfig();
// First populate the configuration from default values then
// environment variables.
auto config = qssc::config::EnvVarConfigBuilder().buildConfig();
if (auto err = config.takeError())
// Explicit move required for some systems as automatic move
// is not recognized.
return std::move(err);

// Apply CLI options of top of the configuration constructed above.
if (auto err = qssc::config::CLIConfigBuilder().populateConfig(*config))
// Explicit move required for some systems as automatic move
// is not recognized.
return std::move(err);

// Set this as the configuration for the current context
qssc::config::setContextConfig(context, std::move(*config));

Expand Down
1 change: 1 addition & 0 deletions lib/Config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
qssc_add_library(QSSConfig
QSSConfig.cpp
CLIConfig.cpp
EnvVarConfig.cpp

ADDITIONAL_HEADER_DIRS
${QSSC_INCLUDE_DIR}/Config
Expand Down
41 changes: 41 additions & 0 deletions lib/Config/EnvVarConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===- EnvVarConfigBuilder.cpp - QSSConfig from EnvVars ----* C++*--------===//
//
// (C) Copyright IBM 2023.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
//
//===----------------------------------------------------------------------===//
//
// This file implements building the configuration from environemnt variables.
//
//===----------------------------------------------------------------------===//

#include "Config/EnvVarConfig.h"

#include <cstdlib>

using namespace qssc::config;

llvm::Error EnvVarConfigBuilder::populateConfig(QSSConfig &config) {
if (auto err = populateConfigurationPath_(config))
return err;

if (auto err = populateTarget_(config))
return err;

return llvm::Error::success();
}

llvm::Error EnvVarConfigBuilder::populateConfigurationPath_(QSSConfig &config) {
if (const char *configurationPath = std::getenv("QSSC_TARGET_CONFIG_PATH"))
config.targetConfigPath = configurationPath;
return llvm::Error::success();
}

llvm::Error EnvVarConfigBuilder::populateTarget_(QSSConfig &config) {
if (const char *targetStr = std::getenv("QSSC_TARGET_NAME"))
config.targetName = targetStr;
return llvm::Error::success();
}
6 changes: 6 additions & 0 deletions test/Config/test-config.tst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// RUN: qss-compiler --target Mock --config path/to/config --allow-unregistered-dialect=false --add-target-passes=false --show-config | FileCheck %s --check-prefix CLI
// RUN: QSSC_TARGET_NAME="MockEnv" QSSC_TARGET_CONFIG_PATH="path/to/config/Env" qss-compiler --allow-unregistered-dialect=false --add-target-passes=false --show-config | FileCheck %s --check-prefix ENV

// CLI: targetName: Mock
// CLI: targetConfigPath: path/to/config
// CLI: allowUnregisteredDialects: 0
// CLI: addTargetPasses: 0

// ENV: targetName: MockEnv
// ENV: targetConfigPath: path/to/config/Env
// ENV: allowUnregisteredDialects: 0
// ENV: addTargetPasses: 0

0 comments on commit 07fbafa

Please sign in to comment.