Skip to content

Commit

Permalink
Increase minimum iOS and macOS supported versions
Browse files Browse the repository at this point in the history
- use more std::filesystem

IB-7483

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma committed Sep 1, 2023
1 parent 4eb9a4d commit 7739584
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 121 deletions.
6 changes: 3 additions & 3 deletions build-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if [ "$#" -eq 0 ]; then
echo " target: osx ios iossimulator ioscatalyst androidarm androidarm64 androidx86 androidx86_64"
echo "To control iOS, macOS builds set environment variables:"
echo " minimum deployment target"
echo " - MACOSX_DEPLOYMENT_TARGET=10.15"
echo " - IPHONEOS_DEPLOYMENT_TARGET=12.0"
echo " - MACOSX_DEPLOYMENT_TARGET=11.0"
echo " - IPHONEOS_DEPLOYMENT_TARGET=13.0"
echo " archs to build on macOS/iOS"
echo " - ARCHS=\"x86_64 arm64\" (macOS)"
echo " - ARCHS=\"arm64\" (iOS)"
Expand Down Expand Up @@ -73,7 +73,7 @@ case "$@" in
;;
esac
TARGET_PATH=/Library/libdigidocpp.${TARGET}
: ${IPHONEOS_DEPLOYMENT_TARGET:="12.0"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="13.0"}
export IPHONEOS_DEPLOYMENT_TARGET
CMAKEARGS="
-DCMAKE_OSX_SYSROOT=${SYSROOT} \
Expand Down
12 changes: 6 additions & 6 deletions prepare_osx_build_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ case "$@" in
CONFIGURE="--host=arm-apple-darwin --enable-static --disable-shared --disable-dependency-tracking"
SYSROOT=$(xcrun -sdk iphonesimulator --show-sdk-path)
: ${ARCHS:="x86_64"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="12.0"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="13.0"}
export IPHONEOS_DEPLOYMENT_TARGET
export CFLAGS="-arch ${ARCHS// / -arch } -isysroot ${SYSROOT}"
;;
Expand All @@ -77,7 +77,7 @@ case "$@" in
CONFIGURE="--host=x86_64-apple-darwin --enable-static --disable-shared --disable-dependency-tracking"
SYSROOT=$(xcrun -sdk macosx --show-sdk-path)
: ${ARCHS:="x86_64 arm64"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="12.0"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="13.0"}
export IPHONEOS_DEPLOYMENT_TARGET
export CFLAGS="-arch ${ARCHS// / -arch } -target x86_64-apple-ios-macabi -isysroot ${SYSROOT}"
;;
Expand All @@ -87,7 +87,7 @@ case "$@" in
CONFIGURE="--host=arm-apple-darwin --enable-static --disable-shared --disable-dependency-tracking"
SYSROOT=$(xcrun -sdk iphoneos --show-sdk-path)
: ${ARCHS:="arm64"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="12.0"}
: ${IPHONEOS_DEPLOYMENT_TARGET:="13.0"}
export IPHONEOS_DEPLOYMENT_TARGET
export CFLAGS="-arch ${ARCHS// / -arch } -isysroot ${SYSROOT}"
;;
Expand All @@ -97,7 +97,7 @@ case "$@" in
CONFIGURE="--disable-static --enable-shared --disable-dependency-tracking"
SYSROOT=$(xcrun -sdk macosx --show-sdk-path)
: ${ARCHS:="x86_64 arm64"}
: ${MACOSX_DEPLOYMENT_TARGET:="10.15"}
: ${MACOSX_DEPLOYMENT_TARGET:="11.0"}
export MACOSX_DEPLOYMENT_TARGET
export CFLAGS="-arch ${ARCHS// / -arch } "
;;
Expand Down Expand Up @@ -423,8 +423,8 @@ case "$@" in
echo " tasks: xerces, xalan, openssl, xmlsec, xsd, all, help"
echo "To control iOS, macOS builds set environment variables:"
echo " minimum deployment target"
echo " - MACOSX_DEPLOYMENT_TARGET=10.15"
echo " - IPHONEOS_DEPLOYMENT_TARGET=12.0"
echo " - MACOSX_DEPLOYMENT_TARGET=11.0"
echo " - IPHONEOS_DEPLOYMENT_TARGET=13.0"
echo " archs to build on macOS/iOS"
echo " - ARCHS=\"x86_64 arm64\" (macOS)"
echo " - ARCHS=\"arm64\" (iOS)"
Expand Down
12 changes: 6 additions & 6 deletions src/ASiContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ unique_ptr<iostream> ASiContainer::dataStream(const string &path, const ZipSeria
{
unique_ptr<iostream> data;
if(d->properties[path].size > MAX_MEM_FILE)
data = make_unique<fstream>(File::encodeName(File::tempFileName()).c_str(), fstream::in|fstream::out|fstream::binary|fstream::trunc);
data = make_unique<fstream>(File::tempFileName(), fstream::in|fstream::out|fstream::binary|fstream::trunc);
else
data = make_unique<stringstream>();
z.extract(path, *data);
Expand All @@ -180,7 +180,7 @@ void ASiContainer::addDataFile(const string &path, const string &mediaType)

ZipSerialize::Properties prop { appInfo(), File::modifiedTime(path), File::fileSize(path) };
bool useTempFile = prop.size > MAX_MEM_FILE;
zproperty(File::fileName(path), move(prop));
zproperty(File::fileName(path), std::move(prop));
unique_ptr<istream> is;
if(useTempFile)
{
Expand All @@ -193,15 +193,15 @@ void ASiContainer::addDataFile(const string &path, const string &mediaType)
*data << file.rdbuf();
is.reset(data);
}
addDataFilePrivate(move(is), fileName, mediaType);
addDataFilePrivate(std::move(is), fileName, mediaType);
}

void ASiContainer::addDataFile(unique_ptr<istream> is, const string &fileName, const string &mediaType)
{
addDataFileChecks(fileName, mediaType);
if(fileName.find_last_of("/\\") != string::npos)
THROW("Document file '%s' cannot contain directory path.", fileName.c_str());
addDataFilePrivate(move(is), fileName, mediaType);
addDataFilePrivate(std::move(is), fileName, mediaType);
}

void ASiContainer::addDataFileChecks(const string &fileName, const string &mediaType)
Expand All @@ -218,7 +218,7 @@ void ASiContainer::addDataFileChecks(const string &fileName, const string &media

void ASiContainer::addDataFilePrivate(unique_ptr<istream> is, const string &fileName, const string &mediaType)
{
d->documents.push_back(new DataFilePrivate(move(is), fileName, mediaType));
d->documents.push_back(new DataFilePrivate(std::move(is), fileName, mediaType));
}

/**
Expand Down Expand Up @@ -288,7 +288,7 @@ ZipSerialize::Properties ASiContainer::zproperty(const string &file) const

void ASiContainer::zproperty(const string &file, ZipSerialize::Properties &&prop)
{
d->properties[file] = move(prop);
d->properties[file] = std::move(prop);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/digidoc-tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "util/log.h"

#include <algorithm>
#include <filesystem>
#include <iostream>
#include <optional>
#include <sstream>
Expand Down
102 changes: 26 additions & 76 deletions src/util/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include <algorithm>
#include <ctime>
#include <filesystem>
#include <locale>
#include <sstream>
#include <sys/stat.h>
Expand Down Expand Up @@ -62,7 +61,7 @@ using f_statbuf = struct stat;
using f_utimbuf = struct utimbuf;
#endif

stack<string> File::tempFiles;
stack<fs::path> File::tempFiles;

string File::confPath()
{
Expand All @@ -73,63 +72,21 @@ string File::confPath()
#elif defined(_WIN32)
return dllPath("digidocpp.dll");
#else
return path(env("SNAP"), DIGIDOCPP_CONFIG_DIR "/");
fs::path result;
if(char *var = getenv("SNAP"))
result = fs::path(var);
return (result / DIGIDOCPP_CONFIG_DIR "/").u8string();
#endif
}

#ifndef _WIN32
string File::env(string_view varname)
{
if(char *var = getenv(varname.data()))
return decodeName(var);
return {};
}
#endif

/**
* Encodes path to compatible std lib
* @param fileName path
* @return encoded path
*/
File::f_string File::encodeName(string_view fileName)
{
if(fileName.empty())
return {};
#ifdef __APPLE__
CFStringRef ref = CFStringCreateWithBytesNoCopy({}, (UInt8 *)fileName.data(),
CFIndex(fileName.size()), kCFStringEncodingUTF8, FALSE, kCFAllocatorNull);
string out(fileName.size() * 2, 0);
CFStringGetFileSystemRepresentation(ref, out.data(), CFIndex(out.size()));
CFRelease(ref);
out.resize(strlen(out.c_str()));
#else
f_string out = fs::u8path(fileName);
#endif
return out;
}

/**
* Decodes path from std lib path
* @param localFileName path
* @return decoded path
*/
string File::decodeName(const f_string_view &localFileName)
fs::path File::encodeName(string_view fileName)
{
if(localFileName.empty())
return {};
#ifdef __APPLE__
CFMutableStringRef ref = CFStringCreateMutable(nullptr, 0);
CFStringAppendCString(ref, localFileName.data(), kCFStringEncodingUTF8);
CFStringNormalize(ref, kCFStringNormalizationFormC);

string out(localFileName.size() * 2, 0);
CFStringGetCString(ref, out.data(), CFIndex(out.size()), kCFStringEncodingUTF8);
CFRelease(ref);
out.resize(strlen(out.c_str()));
#else
string out = fs::path(localFileName).u8string();
#endif
return out;
return fs::u8path(fileName);
}

/**
Expand All @@ -140,8 +97,7 @@ string File::decodeName(const f_string_view &localFileName)
*/
bool File::fileExists(const string& path)
{
f_statbuf fileInfo;
return f_stat(encodeName(path).c_str(), &fileInfo) == 0 && (fileInfo.st_mode & S_IFMT) == S_IFREG;
return fs::is_regular_file(fs::u8path(path));
}

#ifdef _WIN32
Expand All @@ -167,14 +123,13 @@ string File::dllPath(string_view dll)
time_t File::modifiedTime(const string &path)
{
f_statbuf fileInfo;
return f_stat(encodeName(path).c_str(), &fileInfo) ? time(nullptr) : fileInfo.st_mtime;
return f_stat(fs::u8path(path).c_str(), &fileInfo) ? time(nullptr) : fileInfo.st_mtime;
}

void File::updateModifiedTime(const string &path, time_t time)
{
f_string _path = encodeName(path);
f_utimbuf u_time { time, time };
if(f_utime(_path.c_str(), &u_time))
if(f_utime(fs::u8path(path).c_str(), &u_time))
THROW("Failed to update file modified time.");
}

Expand All @@ -193,8 +148,7 @@ string File::fileExtension(const string &path)
*/
unsigned long File::fileSize(const string &path)
{
f_statbuf fileInfo;
return f_stat(encodeName(path).c_str(), &fileInfo) ? 0 : (unsigned long)fileInfo.st_size;
return fs::file_size(fs::u8path(path));
}

/**
Expand Down Expand Up @@ -266,26 +220,22 @@ string File::path(string dir, string_view relativePath)
/**
* @return returns temporary filename.
*/
string File::tempFileName()
fs::path File::tempFileName()
{
#ifdef _WIN32
// requires TMP environment variable to be set
wchar_t *fileName = _wtempnam(nullptr, nullptr); // TODO: static buffer, not thread-safe
if(!fileName)
THROW("Failed to create a temporary file name.");
string path = fs::path(fileName).u8string();
tempFiles.emplace(fileName);
free(fileName);
#else
#ifdef __APPLE__
string path = File::path(env("TMPDIR"), "XXXXXX");
#else
string path = "/tmp/XXXXXX";
#endif
if(mkstemp(path.data()) == -1)
string tmp = "XXXXXX";
if(mkstemp(tmp.data()) == -1)
THROW("Failed to create a temporary file name.");
tempFiles.push(fs::temp_directory_path() / tmp);
#endif
tempFiles.push(path);
return path;
return tempFiles.top();
}

/**
Expand All @@ -300,7 +250,7 @@ void File::createDirectory(string path)
THROW("Can not create directory with no name.");
if(path.back() == '/' || path.back() == '\\')
path.pop_back();
f_string _path = encodeName(path);
auto _path = fs::u8path(path);
#ifdef _WIN32
int result = _wmkdir(_path.c_str());
#else
Expand All @@ -327,7 +277,9 @@ string File::digidocppPath()
CoTaskMemFree(knownFolder);
return appData;
#elif defined(ANDROID)
return path(env("HOME"), ".digidocpp");
if(char *var = getenv("HOME"))
return (fs::path(var) / ".digidocpp").u8string();
return {};
#else
string buf(sysconf(_SC_GETPW_R_SIZE_MAX), 0);
passwd pwbuf {};
Expand Down Expand Up @@ -363,21 +315,19 @@ string File::fullPathUrl(const string &path)
*/
void File::deleteTempFiles()
{
error_code ec;
while(!tempFiles.empty())
{
if(!removeFile(tempFiles.top()))
WARN( "Tried to remove the temporary file or directory '%s', but failed.", tempFiles.top().c_str() );
if(!fs::remove(tempFiles.top(), ec) || ec)
WARN("Tried to remove the temporary file or directory '%s', but failed.", tempFiles.top().u8string().c_str());
tempFiles.pop();
}
}

bool File::removeFile(const string &path)
{
#ifdef _WIN32
return _wremove(fs::u8path(path).c_str()) == 0;
#else
return remove(encodeName(path).c_str()) == 0;
#endif
error_code ec;
return fs::remove(fs::u8path(path), ec);
}

/**
Expand Down
19 changes: 4 additions & 15 deletions src/util/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "../Exception.h"

#include <filesystem>
#include <stack>

namespace digidoc
Expand All @@ -34,18 +35,9 @@ namespace digidoc
class File
{
public:
#ifdef _WIN32
using f_string = std::wstring;
using f_string_view = std::wstring_view;
#else
using f_string = std::string;
using f_string_view = std::string_view;
#endif
static std::string confPath();
static std::string digidocppPath();
static f_string encodeName(std::string_view fileName);
static std::string decodeName(const f_string_view &localFileName);
static bool isRelative(const std::string &path);
static std::filesystem::path encodeName(std::string_view fileName);
static time_t modifiedTime(const std::string &path);
static void updateModifiedTime(const std::string &path, time_t time);
static bool fileExists(const std::string& path);
Expand All @@ -55,7 +47,7 @@ namespace digidoc
static std::string directory(const std::string& path);
static std::string path(std::string dir, std::string_view relativePath);
static std::string fullPathUrl(const std::string &path);
static std::string tempFileName();
static std::filesystem::path tempFileName();
static void createDirectory(std::string path);
static void deleteTempFiles();
static bool removeFile(const std::string &path);
Expand All @@ -71,10 +63,7 @@ namespace digidoc
#ifdef __APPLE__
static std::string frameworkResourcesPath(std::string_view name);
#endif
static std::stack<std::string> tempFiles;
#ifndef _WIN32
static std::string env(std::string_view varname);
#endif
static std::stack<std::filesystem::path> tempFiles;
};

}
Expand Down
Loading

0 comments on commit 7739584

Please sign in to comment.