Skip to content

Commit

Permalink
cmake: Add support for FHS paths for docs and data
Browse files Browse the repository at this point in the history
This is needed to make Linux distributions happy.
  • Loading branch information
jtojnar committed Mar 12, 2022
1 parent cc05555 commit 4b834f0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 39 deletions.
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ script:
make -C _build DESTDIR=appdir -j$(nproc) install
wget -c -nv "https://github.com/probonopd/linuxdeployqt/releases/download/continuous/linuxdeployqt-continuous-x86_64.AppImage"
chmod a+x linuxdeployqt-continuous-x86_64.AppImage
# FIXME: The following reshuffling can be removed once https://github.com/aardappel/treesheets/issues/133 is resolved
mkdir -p appdir/usr/bin/ # FIXME
mv appdir/usr/docs appdir/usr/bin/ # FIXME
mv appdir/usr/examples appdir/usr/bin/ # FIXME
mv appdir/usr/treesheets appdir/usr/bin/ # FIXME
strip _build/treesheets # FIXME
cp _build/treesheets appdir/usr/bin/ # FIXME
ls -lh appdir/usr/bin/treesheets && chmod a+x appdir/usr/bin/treesheets
mv appdir/usr/scripts appdir/usr/bin/ # FIXME
mv appdir/usr/images appdir/usr/bin/ # FIXME
( cd appdir ; ln -s usr/bin/docs/ usr/bin/examples/ usr/bin/images/ usr/bin/scripts/ . )
sed -i -e 's|1011|1002|g' appdir/usr/bin/treesheets # https://github.com/aardappel/treesheets/issues/130#issuecomment-528075693
# Workaround to increase compatibility with older systems; see https://github.com/darealshinji/AppImageKit-checkrt for details
mkdir -p appdir/usr/optional/ ; wget -c https://github.com/darealshinji/AppImageKit-checkrt/releases/download/continuous/exec-x86_64.so -O ./appdir/usr/optional/exec.so
Expand Down
25 changes: 18 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,29 @@ add_executable(treesheets ${sources})

target_link_libraries(treesheets PRIVATE ${wxWidgets_LIBRARIES})
if(NOT APPLE AND NOT WIN32 AND NOT TREESHEETS_RELOCATABLE_INSTALLATION)
set(TREESHEETS_BINDIR ${CMAKE_INSTALL_BINDIR})
set(TREESHEETS_DOCDIR ${CMAKE_INSTALL_DOCDIR})
set(TREESHEETS_FULL_DOCDIR ${CMAKE_INSTALL_FULL_DOCDIR})
set(TREESHEETS_PKGDATADIR ${CMAKE_INSTALL_DATADIR}/${CMAKE_PROJECT_NAME})
set(TREESHEETS_FULL_PKGDATADIR ${CMAKE_INSTALL_FULL_DATADIR}/${CMAKE_PROJECT_NAME})

# Apple and Windows always look to relative paths for locales.
target_compile_definitions(treesheets PRIVATE "LOCALEDIR=L\"${CMAKE_INSTALL_FULL_LOCALEDIR}\"")
target_compile_definitions(treesheets PRIVATE "TREESHEETS_DOCDIR=\"${TREESHEETS_FULL_DOCDIR}\"")
target_compile_definitions(treesheets PRIVATE "TREESHEETS_DATADIR=\"${TREESHEETS_FULL_PKGDATADIR}\"")
else()
set(TREESHEETS_BINDIR ${CMAKE_INSTALL_PREFIX})
set(TREESHEETS_DOCDIR ${CMAKE_INSTALL_PREFIX})
set(TREESHEETS_PKGDATADIR ${CMAKE_INSTALL_PREFIX})
endif()

install(TARGETS treesheets DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY TS/docs DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES TS/readme.html DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS treesheets DESTINATION ${TREESHEETS_BINDIR})
install(DIRECTORY TS/docs DESTINATION ${TREESHEETS_DOCDIR})
install(FILES TS/readme.html DESTINATION ${TREESHEETS_DOCDIR})
install(DIRECTORY TS/examples DESTINATION ${TREESHEETS_DOCDIR})

install(DIRECTORY TS/examples DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY TS/images DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY TS/images DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY TS/scripts DESTINATION ${CMAKE_INSTALL_PREFIX})
install(DIRECTORY TS/images DESTINATION ${TREESHEETS_PKGDATADIR})
install(DIRECTORY TS/scripts DESTINATION ${TREESHEETS_PKGDATADIR})

# Install translations to correct platform-specific path.
# See: https://docs.wxwidgets.org/trunk/overview_i18n.html#overview_i18n_mofiles
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ Linux:
- Using the version of wxWidgets from https://github.com/wxWidgets/wxWidgets.git
- Follow the instructions to build there, but add `--enable-unicode` and
`--disabled-shared` to the `configure` step.
- Build with `cmake -S . -B _build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/treesheets` or similar.
- Install using `sudo make -C _build install`. Note that, currently, everything is installed into the same directory,
since TreeSheets will look for its files relative to the binary.
- Build with `cmake -S . -B _build -DCMAKE_BUILD_TYPE=Release` or similar.
You can change the default installation prefix (`/usr/local`) by passing something like `-DCMAKE_INSTALL_PREFIX=/usr`.
- Install using `sudo make -C _build install`.
- There is also a `src/Makefile`, this is deprecated.

OSX:
Expand Down
4 changes: 2 additions & 2 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,9 @@ struct Document {
case A_HELP:
#ifdef __WXMAC__
wxLaunchDefaultBrowser(L"file://" +
sys->frame->GetPath(L"docs/tutorial.html")); // RbrtPntn
sys->frame->GetDocPath(L"docs/tutorial.html")); // RbrtPntn
#else
wxLaunchDefaultBrowser(sys->frame->GetPath(L"docs/tutorial.html"));
wxLaunchDefaultBrowser(sys->frame->GetDocPath(L"docs/tutorial.html"));
#endif
return nullptr;

Expand Down
54 changes: 42 additions & 12 deletions src/myframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,39 @@ struct MyFrame : wxFrame {
ColorDropdown *textdd = nullptr;
ColorDropdown *borddd = nullptr;

wxString GetPath(const wxString &relpath) {
if (!exepath_.Length()) return relpath;
return exepath_ + "/" + relpath;
wxString GetDocPath(const wxString &relpath) {
std::filesystem::path candidatePaths[] = {
std::filesystem::path(exepath_.Length() ? exepath_.ToStdString() + "/" + relpath.ToStdString() : relpath.ToStdString()),
#ifdef TREESHEETS_DOCDIR
std::filesystem::path(TREESHEETS_DOCDIR "/" + relpath.ToStdString()),
#endif
};
std::filesystem::path relativePath;
for (auto path : candidatePaths) {
relativePath = path;
if (std::filesystem::exists(relativePath)) {
break;
}
}

return wxString(relativePath.c_str());
}
wxString GetDataPath(const wxString &relpath) {
std::filesystem::path candidatePaths[] = {
std::filesystem::path(exepath_.Length() ? exepath_.ToStdString() + "/" + relpath.ToStdString() : relpath.ToStdString()),
#ifdef TREESHEETS_DATADIR
std::filesystem::path(TREESHEETS_DATADIR "/" + relpath.ToStdString()),
#endif
};
std::filesystem::path relativePath;
for (auto path : candidatePaths) {
relativePath = path;
if (std::filesystem::exists(relativePath)) {
break;
}
}

return wxString(relativePath.c_str());
}

MenuString menustrings;
Expand Down Expand Up @@ -91,7 +121,7 @@ struct MyFrame : wxFrame {

wxLogMessage(L"locale: %s", std::setlocale(LC_CTYPE, nullptr));

app->AddTranslation(GetPath("translations"));
app->AddTranslation(GetDataPath("translations"));

csf = GetContentScaleFactor();
wxLogMessage(L"content scale: %f", csf);
Expand Down Expand Up @@ -121,8 +151,8 @@ struct MyFrame : wxFrame {

wxIconBundle icons;
wxIcon iconbig;
icon.LoadFile(GetPath(L"images/icon16.png"), wxBITMAP_TYPE_PNG);
iconbig.LoadFile(GetPath(L"images/icon32.png"), wxBITMAP_TYPE_PNG);
icon.LoadFile(GetDataPath(L"images/icon16.png"), wxBITMAP_TYPE_PNG);
iconbig.LoadFile(GetDataPath(L"images/icon32.png"), wxBITMAP_TYPE_PNG);
if (!icon.IsOk() || !iconbig.IsOk()) {
wxMessageBox(_(L"Error loading core data file (TreeSheets not installed correctly?)"),
_(L"Initialization Error"), wxOK, this);
Expand All @@ -139,9 +169,9 @@ struct MyFrame : wxFrame {
SetIcons(icons);

wxImage foldiconi;
line_nw.LoadFile(GetPath(L"images/render/line_nw.png"), wxBITMAP_TYPE_PNG);
line_sw.LoadFile(GetPath(L"images/render/line_sw.png"), wxBITMAP_TYPE_PNG);
foldiconi.LoadFile(GetPath(L"images/nuvola/fold.png"));
line_nw.LoadFile(GetDataPath(L"images/render/line_nw.png"), wxBITMAP_TYPE_PNG);
line_sw.LoadFile(GetDataPath(L"images/render/line_sw.png"), wxBITMAP_TYPE_PNG);
foldiconi.LoadFile(GetDataPath(L"images/nuvola/fold.png"));
foldicon = wxBitmap(foldiconi);
ScaleBitmap(foldicon, csf / 3.0, foldicon);

Expand Down Expand Up @@ -547,7 +577,7 @@ struct MyFrame : wxFrame {
optmenu->AppendSubMenu(roundmenu, _(L"&Roundness of grid borders..."));

wxMenu *scriptmenu = new wxMenu();
auto scriptpath = GetPath("scripts/");
auto scriptpath = GetDataPath("scripts/");
wxString sf = wxFindFirstFile(scriptpath + L"*.lobster");
int sidx = 0;
while (!sf.empty()) {
Expand Down Expand Up @@ -630,7 +660,7 @@ struct MyFrame : wxFrame {
#endif

wxString iconpath =
GetPath(iconset ? L"images/webalys/toolbar/" : L"images/nuvola/toolbar/");
GetDataPath(iconset ? L"images/webalys/toolbar/" : L"images/nuvola/toolbar/");
auto sz = (iconset ? wxSize(18, 18) : wxSize(22, 22)) * csf;
tb->SetToolBitmapSize(sz);

Expand Down Expand Up @@ -684,7 +714,7 @@ struct MyFrame : wxFrame {
tb->AddControl(borddd);
tb->AddSeparator();
tb->AddControl(new wxStaticText(tb, wxID_ANY, _(L"Image ")));
wxString imagepath = GetPath("images/nuvola/dropdown/");
wxString imagepath = GetDataPath("images/nuvola/dropdown/");
idd = new ImageDropdown(tb, imagepath);
tb->AddControl(idd);
tb->Realize();
Expand Down
1 change: 1 addition & 0 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ WX_DECLARE_STRING_HASH_MAP(bool, wxHashMapBool);
#include <algorithm>
#include <memory>

#include <filesystem>
#include <utility>

#include <locale>
Expand Down
6 changes: 3 additions & 3 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ struct System {
void LoadTut() {
auto lang = frame->app->locale.GetCanonicalName();
lang.Truncate(2);
if (LoadDB(frame->GetPath(L"examples/tutorial-" + lang + ".cts"))[0]) {
LoadDB(frame->GetPath(L"examples/tutorial.cts"));
if (LoadDB(frame->GetDocPath(L"examples/tutorial-" + lang + ".cts"))[0]) {
LoadDB(frame->GetDocPath(L"examples/tutorial.cts"));
}
}

void LoadOpRef() { LoadDB(frame->GetPath(L"examples/operation-reference.cts")); }
void LoadOpRef() { LoadDB(frame->GetDocPath(L"examples/operation-reference.cts")); }

Cell *&InitDB(int sizex, int sizey = 0) {
Cell *c = new Cell(nullptr, nullptr, CT_DATA, new Grid(sizex, sizey ? sizey : sizex));
Expand Down
2 changes: 1 addition & 1 deletion src/treesheets_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static int64_t TreeSheetsLoader(std::string_view absfilename, std::string *dest,
static TreeSheetsScriptImpl tssi;

static void ScriptInit(MyFrame *frame) {
auto serr = InitLobster(&tssi, frame->GetPath("scripts/"), "", false, TreeSheetsLoader);
auto serr = InitLobster(&tssi, frame->GetDataPath("scripts/"), "", false, TreeSheetsLoader);
if (!serr.empty())
frame->GetCurTab()->Status(wxString("Script system could not initialize: " + serr));
}

0 comments on commit 4b834f0

Please sign in to comment.