Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr committed Aug 7, 2024
1 parent cde8dee commit b5510c0
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 93 deletions.
7 changes: 6 additions & 1 deletion .devcontainer/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ if ! command -v clang-format; then
fi

if ! command -v clang; then
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
sudo apt update
sudo apt install -y clang
fi

if ! command -v clang-19; then
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" -- 19
fi

if ! command -v gcc; then
Expand Down
Empty file.
44 changes: 44 additions & 0 deletions .github/workflows/cmake-workflow-preset-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: cmake --workflow --preset test
on:
push:
branches: ["main"]
paths-ignore:
- .gitignore
- LICENSE
- README.md
- .github/**
- "!.github/workflows/cmake-workflow-preset-test.yml"
pull_request:
paths-ignore:
- .gitignore
- LICENSE
- README.md
- .github/**
- "!.github/workflows/cmake-workflow-preset-test.yml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
cmake-workflow-preset-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-13, macos-latest, windows-latest]
defaults:
run:
shell: bash
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: jwlawson/actions-setup-cmake@v2
- uses: goto-bus-stop/setup-zig@v2
- run: cmake --workflow --preset package
- if: runner.os == 'Windows'
run: unzip build/platformdirs-*.zip -d stage
- if: runner.os != 'Windows'
run: tar -xzvf build/platformdirs-*.tar.gz -C stage
- uses: actions/upload-artifact@v4
with:
name: platformdirs-${{ matrix.target }}
path: stage
- run: cmake --workflow --preset test
28 changes: 11 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ project(
HOMEPAGE_URL "https://jcbhmr.me/platformdirs"
LANGUAGES CXX)

# Main dependencies
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 11.0.2
FIND_PACKAGE_ARGS 11.0.2...<12)
# FMT_OS doesn't work with Cosmopolitan
set(FMT_OS OFF)
FetchContent_MakeAvailable(fmt)

# Library targets
add_library(platformdirs)
add_library(platformdirs::platformdirs ALIAS platformdirs)
Expand All @@ -35,7 +24,6 @@ target_include_directories(platformdirs PUBLIC include)
target_compile_features(platformdirs PRIVATE cxx_std_23)
# cosmocc has exceptions off by default
target_compile_options(platformdirs PRIVATE -fexceptions)
target_link_libraries(platformdirs PRIVATE fmt::fmt)
target_compile_definitions(platformdirs PRIVATE
PROJECT_VERSION="${PROJECT_VERSION}"
PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
Expand All @@ -51,7 +39,7 @@ target_sources(platformdirs_exe PRIVATE src/platformdirs_exe.cpp)
target_compile_features(platformdirs_exe PRIVATE cxx_std_23)
# cosmocc has exceptions off by default
target_compile_options(platformdirs_exe PRIVATE -fexceptions)
target_link_libraries(platformdirs_exe PRIVATE fmt::fmt platformdirs)
target_link_libraries(platformdirs_exe PRIVATE platformdirs)
target_compile_definitions(platformdirs_exe PRIVATE
PROJECT_VERSION="${PROJECT_VERSION}"
PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
Expand All @@ -65,12 +53,18 @@ if(BUILD_TESTING)
add_test(NAME platformdirs COMMAND platformdirs_exe)
endif()

# Packaging
include(CPack)

# Installation
include(GNUInstallDirs)
install(TARGETS platformdirs platformdirs_exe)
install(TARGETS platformdirs_exe)

# Packaging
if(WIN32)
set(CPACK_GENERATOR ZIP)
else()
set(CPACK_GENERATOR TGZ)
endif()
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
include(CPack)

# Tasks
if(BUILD_TESTING)
Expand Down
65 changes: 65 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"toolchainFile": "cmake/clang.cmake",
"binaryDir": "build/clang"
},
{
"name": "clang-19",
"inherits": ["clang"],
"toolchainFile": "cmake/clang-19.cmake",
"binaryDir": "build/clang-19"
},
{
"name": "cosmocc",
"inherits": ["default"],
Expand Down Expand Up @@ -100,6 +106,11 @@
"inherits": ["default"],
"configurePreset": "clang"
},
{
"name": "clang-19",
"inherits": ["clang"],
"configurePreset": "clang-19"
},
{
"name": "cosmocc",
"inherits": ["default"],
Expand Down Expand Up @@ -184,6 +195,21 @@
"name": "default",
"configurePreset": "default"
},
{
"name": "clang",
"inherits": ["default"],
"configurePreset": "clang"
},
{
"name": "clang-19",
"inherits": ["clang"],
"configurePreset": "clang-19"
},
{
"name": "zig-cc",
"inherits": ["default"],
"configurePreset": "zig-cc"
},
{
"name": "release",
"inherits": ["default"],
Expand Down Expand Up @@ -221,13 +247,52 @@
{ "type": "test", "name": "cosmocc" }
]
},
{
"name": "package",
"steps": [
{ "type": "configure", "name": "default" },
{ "type": "build", "name": "default" },
{ "type": "package", "name": "default" }
]
},
{
"name": "package-clang",
"steps": [
{ "type": "configure", "name": "clang" },
{ "type": "build", "name": "clang" },
{ "type": "package", "name": "clang" }
]
},
{
"name": "package-clang-19",
"steps": [
{ "type": "configure", "name": "clang-19" },
{ "type": "build", "name": "clang-19" },
{ "type": "package", "name": "clang-19" }
]
},
{
"name": "package-zig-cc",
"steps": [
{ "type": "configure", "name": "zig-cc" },
{ "type": "build", "name": "zig-cc" },
{ "type": "package", "name": "zig-cc" }
]
},
{
"name": "clang",
"steps": [
{ "type": "configure", "name": "clang" },
{ "type": "build", "name": "clang" }
]
},
{
"name": "clang-19",
"steps": [
{ "type": "configure", "name": "clang-19" },
{ "type": "build", "name": "clang-19" }
]
},
{
"name": "cosmocc",
"steps": [
Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@

<table align=center><td>

TODO
```cpp
auto dirs = platformdirs::platform_dirs(app_name, app_author, "1.0");
std::println("user_data_dir: {}", dirs.user_data_dir());
std::println("user_config_dir: {}", dirs.user_config_dir());
std::println("user_cache_dir: {}", dirs.user_cache_dir());
std::println("user_state_dir: {}", dirs.user_state_dir());
std::println("user_log_dir: {}", dirs.user_log_dir());
std::println("...and more!");
```
```
user_data_dir: ~/.local/share/MyApp/1.0
user_config_dir: ~/.config/MyApp/1.0
user_cache_dir: ~/.cache/MyApp/1.0
user_state_dir: ~/.local/state/MyApp/1.0
user_log_dir: ~/.local/state/MyApp/1.0/log
...and more!
```
</table>
🐍 Same API surface as [platformdirs](https://pypi.org/project/platformdirs/) \
🔺 Works great with CMake \
💅 Uses platformdirs' opinionated directories \
📂 Works on Windows \
🌌 Works with [Cosmopolitan Libc](https://github.com/jart/cosmopolitan)!
🌌 Works with [cosmocc](https://github.com/jart/cosmopolitan/tree/master/tool/cosmocc)!
## Installation
![CMake](https://img.shields.io/static/v1?style=for-the-badge&message=CMake&color=064F8C&logo=CMake&logoColor=FFFFFF&label=)
<dl>
<dt>CMake <code>find_package()</code> with <code>FetchContent</code> fallback
<dd>
Expand All @@ -40,6 +59,8 @@ target_link_libraries(mylib platformdirs::platformdirs)

## Usage

![C++](https://img.shields.io/static/v1?style=for-the-badge&message=C%2B%2B&color=00599C&logo=C%2B%2B&logoColor=FFFFFF&label=)

<div><code>main.cpp</code></div>

```cpp
Expand All @@ -58,6 +79,8 @@ int main() {

## Development

![CMake](https://img.shields.io/static/v1?style=for-the-badge&message=CMake&color=064F8C&logo=CMake&logoColor=FFFFFF&label=)

```sh
cmake --workflow --preset default
cmake --workflow --preset test
Expand Down
5 changes: 5 additions & 0 deletions cmake/clang-19.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(CMAKE_ASM_COMPILER clang-19)
set(CMAKE_C_COMPILER clang-19)
set(CMAKE_CXX_COMPILER clang++-19)
find_program(CMAKE_AR llvm-ar-19 REQUIRED)
find_program(CMAKE_RANLIB llvm-ranlib-19 REQUIRED)
2 changes: 1 addition & 1 deletion src/platformdirs.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#undef unix
#include "platformdirs.h"
#include <filesystem>
#include <fmt/format.h>
#include <format>
#include <memory>
#include <string>

Expand Down
6 changes: 3 additions & 3 deletions src/platformdirs/windows.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "windows.h"
#include "utils.h"
#include <filesystem>
#include <fmt/format.h>
#include <format>
#include <optional>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -152,13 +152,13 @@ auto platformdirs::windows::get_win_folder_from_env_vars(
} else if (csidl_name == "CSIDL_LOCAL_APPDATA") {
env_var_name = "LOCALAPPDATA";
} else {
throw std::runtime_error(fmt::format("Unknown CSIDL name: {}", csidl_name));
throw std::runtime_error(std::format("Unknown CSIDL name: {}", csidl_name));
}

auto result_c_str = std::getenv(env_var_name.c_str());
if (!result_c_str) {
throw std::runtime_error(
fmt::format("Unset environment variable: {}", env_var_name));
std::format("Unset environment variable: {}", env_var_name));
}
return std::string(result_c_str);
}
Expand Down
Loading

0 comments on commit b5510c0

Please sign in to comment.