Giter Site home page Giter Site logo

config.ml's Introduction

config.ml

Ergonomic, lightweight conditional compilation through attributes inspired by Rust's cfg macro.

Config implements the [@@config] annotation, to conditionally compile modules. You can use a small boolean language to check for conditions including some built-in ones and environment variables.

It can be used to conditionally include/exclude:

  • modules
  • let bindings
  • includes
  • type definitions
  • constructors from variant types and polymorphic variants
  • fields from record types
  • and more

Currently includes/detects the following platforms:

  • Windows 32/64
  • macOS/tvOS/watchOS/iOS
  • FreeBSD/NetBSD
  • Android
  • Linux

And the following architectures:

  • x86-64
  • x86 i386
  • aarch64
  • arm

And the following environment:

  • msvc
  • gnu
  • musl

Getting Started

opam install config

And add it to your dune files:

(library
  (name my_lib)
  (preprocess (pps config.ppx)))

And tag your values with the @@config or @@cfg attribute:

module A = A_unix [@@config any(target_os = "macos", target_os = "linux")]
module A = A_win32 [@@cfg any(target_os = "windows")]

The Config Language

Config implements a very small boolean language with 4 operations:

  • var = value, that checks if a variable (from the environment or provided by config) is equals to a value. Equality is string or integer equality.
  • all(expr1, expr2, ...), expects all expressions to be true
  • any(expr1, expr2, ...), expects any expression to be true
  • not(expr), negates an expression

To define new variables you can pass them in as environment variables:

; export BAND="rush" dune build

And you'll be able to write (BAND = "rush")

Cookbook

Enabling/Disabling Modules and Includes

module Pro_mode_env = struct
  let name = "pro-mode"
  let coins = 2112
end
[@@config (project_mode = "pro")]

include Pro_mode_env
[@@config (project_mode = "pro")]

Enabling/Disabling Let definitions

let favorite_band = "rush"
[@@config (is_rush_fan = true)]

let favorite_band = "unknown"
[@@config not (is_rush_fan = true)]

Enabling/Disabling Externals

external dog_bark : unit -> int = "dog_bark"
[@@config (includes = "dog")]

Enabling/Disabling Types

type band = { name: string }
[@@config (use_band = true)]

Enabling/Disabling Variant Constructors

type favorite_bands = 
  | Rush
  | Yes
  | KingCrimson [@config (likes_trumpets = true)]

type has_published_album_recently = 
  [ `nope
  | `yes
  | `maybe [@config (is_indecisive = true)]
  ]

Enabling/Disabling Record Fields

type user = {
  name: string;
  pass: string [@config (password_mode = "clear")];
  pass: Password.t [@config (password_mode = "encrypted")];
}

Enabling/Disabling entire Modules

[@@@config (should_include = "no")]

let do_not_include_me = failwith "oh no"

Contributing

Currently config relies on C preprocessor definitions to detect different platforms and architectures. They are straightforward to add by extending the file ./config/config_stubs.cl. In there :

  • To add a new platform, extend the main if-else chain in the caml_config_target_os function to include your OS.

  • To add a new architecture, extend the main if-else chain in the caml_config_target_arch function to include your architecture.

config.ml's People

Contributors

leostera avatar gpetiot avatar kfoxder avatar metame avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.