Skip to content

Commit

Permalink
Apothecary Update / Project Generator Update (#8068)
Browse files Browse the repository at this point in the history
* Apothecary Update / Project Generator Update
- osx libraries now using /macos/ folder

* macos make file fix

* macos paths

* fmod off default

* ofAppNoWindow macOS  has issue converting pixels without GL context - io test use window for now -

* cURL debugging

* embedded curl test

* cURL Tests macOS fixes

* ofURLFileLoader - OpenSSL Local cert when targeting macOS

* Tests - test no window again

* Tests - io loadImage changes reset

* FMOD Re-enabled for default
  • Loading branch information
danoli3 authored Aug 5, 2024
1 parent fb3bfef commit 252297e
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,5 @@ scripts/templates/ios/bin
libs/openFrameworksCompiled/project/tvOS/tvOS+OFLib.xcodeproj/xcshareddata
scripts/osx/regressionTest
examples/**/**/[Cc]onfig.make
*.key
*.pem
128 changes: 124 additions & 4 deletions libs/openFrameworks/utils/ofURLFileLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#include "ofAppRunner.h"
#include "ofURLFileLoader.h"
#include "ofUtils.h"
#include "ofFileUtils.h"
#include "ofLog.h"

using std::map;
using std::set;
using std::string;

#if !defined(TARGET_IMPLEMENTS_URL_LOADER)
#include <curl/curl.h>
#include "ofThreadChannel.h"
#include "ofThread.h"
#include <curl/curl.h>
#include "ofThreadChannel.h"
#include "ofThread.h"
static bool curlInited = false;
#if !defined(NO_OPENSSL)
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <iostream>
#include <fstream>
#define CERTIFICATE_FILE "ssl/cacert.pem"
#define PRIVATE_KEY_FILE "ssl/cacert.key"
#endif
#endif

int ofHttpRequest::nextID = 0;
Expand All @@ -32,6 +45,10 @@ class ofURLFileLoaderImpl : public ofThread, public ofBaseURLFileLoader {
void remove(int id);
void clear();
void stop();
#if !defined(NO_OPENSSL)
bool checkValidCertifcate(const std::string& cert_file);
void createSSLCertificate();
#endif
ofHttpResponse handleRequest(const ofHttpRequest & request);
int handleRequestAsync(const ofHttpRequest & request); // returns id

Expand Down Expand Up @@ -110,6 +127,87 @@ void ofURLFileLoaderImpl::stop() {
waitForThread();
}

#if !defined(NO_OPENSSL)
bool ofURLFileLoaderImpl::checkValidCertifcate(const std::string& cert_file) {
FILE *fp = fopen(cert_file.c_str(), "r");
if (!fp) return false;
X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL);
fclose(fp);
if (!cert) return false;
time_t current_time = time(NULL);
int notBefore = X509_cmp_time(X509_get0_notBefore(cert), &current_time);
int notAfter = X509_cmp_time(X509_get0_notAfter(cert), &current_time);
X509_free(cert);
return (notBefore <= 0 && notAfter >= 0);
}

void ofURLFileLoaderImpl::createSSLCertificate() {
EVP_PKEY *pkey = nullptr;
X509 *x509 = nullptr;
EVP_PKEY_CTX *pkey_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (!pkey_ctx) {
ofLogError("ofURLFileLoader") << "Error initializing key generation context";
return;
}
if (EVP_PKEY_keygen_init(pkey_ctx) <= 0 ||
EVP_PKEY_CTX_set_rsa_keygen_bits(pkey_ctx, 2048) <= 0 ||
EVP_PKEY_keygen(pkey_ctx, &pkey) <= 0) {
ofLogError("ofURLFileLoader") << "Error generating RSA key";
EVP_PKEY_CTX_free(pkey_ctx);
return;
}
EVP_PKEY_CTX_free(pkey_ctx);
x509 = X509_new();
ASN1_INTEGER_set(X509_get_serialNumber(x509), 1);
X509_gmtime_adj(X509_get_notBefore(x509), 0);
X509_gmtime_adj(X509_get_notAfter(x509), 31536000L); // 1 year == 31536000L
X509_set_pubkey(x509, pkey);
X509_NAME *name = X509_get_subject_name(x509);
X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"US", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char *)"Local Machine", -1, -1, 0);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)"Local Root CA", -1, -1, 0);
X509_set_issuer_name(x509, name);
if (X509_sign(x509, pkey, EVP_sha256()) == 0) {
ofLogError("ofURLFileLoader") << "Error signing the certificate";
EVP_PKEY_free(pkey);
X509_free(x509);
return;
}
BIO *keyBio = BIO_new(BIO_s_mem());
BIO *certBio = BIO_new(BIO_s_mem());
PEM_write_bio_PrivateKey(keyBio, pkey, nullptr, nullptr, 0, nullptr, nullptr);
PEM_write_bio_X509(certBio, x509);
char *keyData = nullptr;
long keyLen = BIO_get_mem_data(keyBio, &keyData);
std::string keyStr(keyData, keyLen);
char *certData = nullptr;
long certLen = BIO_get_mem_data(certBio, &certData);
std::string certStr(certData, certLen);
ofBuffer keyBuffer;
ofBuffer certBuffer;
keyBuffer.set(keyStr.c_str(), keyLen);
certBuffer.set(certStr.c_str(), certLen);

if(!ofDirectory::createDirectory( "ssl" )) {
ofLogWarning("ofURLFileLoader") << "ssl dir could not create";
}
if(!ofBufferToFile(ofToDataPath(PRIVATE_KEY_FILE), keyBuffer)) {
ofLogError("ofURLFileLoader") << "createSSLCertificate. could not save keyBuffer";
}
if(!ofBufferToFile(ofToDataPath(CERTIFICATE_FILE), certBuffer)) {
ofLogError("ofURLFileLoader") << "createSSLCertificate. could not save certBuffer";
}

BIO_free(keyBio);
BIO_free(certBio);
EVP_PKEY_free(pkey);
X509_free(x509);

ofLogNotice("ofURLFileLoader") << "Root certificate and private key generated and saved";
}
#endif


void ofURLFileLoaderImpl::threadedFunction() {
setThreadName("ofURLFileLoader " + ofToString(getThreadId()));
while (isThreadRunning()) {
Expand Down Expand Up @@ -172,7 +270,29 @@ size_t readBody_cb(void * ptr, size_t size, size_t nmemb, void * userdata) {
ofHttpResponse ofURLFileLoaderImpl::handleRequest(const ofHttpRequest & request) {
std::unique_ptr<CURL, void (*)(CURL *)> curl = std::unique_ptr<CURL, void (*)(CURL *)>(curl_easy_init(), curl_easy_cleanup);
curl_slist * headers = nullptr;
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, true);
#ifdef CURL_DEBUG
curl_version_info_data *version = curl_version_info( CURLVERSION_NOW );
CURLcode ret = curl_easy_setopt(curl.get(), CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl.get(), CURLOPT_USERAGENT, "curl/8.9.1");
#endif
#ifdef TARGET_OSX
#if !defined(NO_OPENSSL)
const std::string caPath = "ssl";
const std::string caFile = "ssl/cacert.pem";
if (ofFile::doesFileExist(ofToDataPath(CERTIFICATE_FILE)) && checkValidCertifcate(ofToDataPath(CERTIFICATE_FILE))) {
ofLogVerbose("ofURLFileLoader") << "SSL valid certificate found";
} else {
ofLogVerbose("ofURLFileLoader") << "SSL certificate not found - generating";
createSSLCertificate();
}
curl_easy_setopt(curl.get(), CURLOPT_CAPATH, ofToDataPath(caPath, true).c_str());
curl_easy_setopt(curl.get(), CURLOPT_CAINFO, ofToDataPath(caFile, true).c_str());
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, false);
#endif
#else
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 1L);
#endif
curl_easy_setopt(curl.get(), CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(curl.get(), CURLOPT_URL, request.url.c_str());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,33 @@ ifndef MAC_OS_STD_LIB
MAC_OS_STD_LIB = libc++
endif

ifndef MAC_OS_C_VER
MAC_OS_C_VER = -std=c17
endif

ifndef MAC_OS_CPP_VER
MAC_OS_CPP_VER = -std=c++17
MAC_OS_CPP_VER = -std=c++23
endif

# Link against libstdc++ to silence tr1/memory errors on latest versions of osx
PLATFORM_CFLAGS = -stdlib=$(MAC_OS_STD_LIB)

# Warning Flags (http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html)
PLATFORM_CFLAGS += -Wall
PLATFORM_CFLAGS += -Wall -Werror=return-type

# Code Generation Option Flags (http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html)
PLATFORM_CFLAGS += -fexceptions

PLATFORM_CFLAGS += -Werror=return-type

ifeq ($(shell xcode-select -print-path 2> /dev/null; echo $$?),0)
MAC_OS_XCODE_ROOT=$(shell xcode-select -print-path)

MAC_OS_SDK_PATH=$(MAC_OS_XCODE_ROOT)/Platforms/MacOSX.platform/Developer/SDKs

ifndef MAC_OS_SDK
ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX14.4.sdk),$(MAC_OS_SDK_PATH)/MacOSX14.4.sdk)
ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX14.5.sdk),$(MAC_OS_SDK_PATH)/MacOSX14.5.sdk)
MAC_OS_SDK=14.5
else ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX14.4.sdk),$(MAC_OS_SDK_PATH)/MacOSX14.4.sdk)
MAC_OS_SDK=14.4
else ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX14.3.sdk),$(MAC_OS_SDK_PATH)/MacOSX14.3.sdk)
MAC_OS_SDK=14.3
Expand Down Expand Up @@ -135,7 +140,7 @@ ifeq ($(shell xcode-select -print-path 2> /dev/null; echo $$?),0)
else ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX11.0.sdk),$(MAC_OS_SDK_PATH)/MacOSX11.0.sdk)
MAC_OS_SDK=11.0
else ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX10.15.sdk),$(MAC_OS_SDK_PATH)/MacOSX10.15.sdk)
MAC_OS_SDK=10.15
MAC_OS_SDK=10.15
else ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX10.14.sdk),$(MAC_OS_SDK_PATH)/MacOSX10.14.sdk)
MAC_OS_SDK=10.14
else ifeq ($(wildcard $(MAC_OS_SDK_PATH)/MacOSX10.13.sdk),$(MAC_OS_SDK_PATH)/MacOSX10.13.sdk)
Expand Down Expand Up @@ -179,7 +184,9 @@ ifdef MAC_OS_SDK_ROOT
endif

PLATFORM_CFLAGS += -mmacosx-version-min=$(MAC_OS_MIN_VERSION)
PLATFORM_CFLAGS += $(MAC_OS_C_VER)

PLATFORM_CXXFLAGS += -mmacosx-version-min=$(MAC_OS_MIN_VERSION)
PLATFORM_CXXFLAGS += -x objective-c++
PLATFORM_CXXFLAGS += $(MAC_OS_CPP_VER)

Expand All @@ -201,9 +208,8 @@ endif
################################################################################

PLATFORM_LDFLAGS = -stdlib=$(MAC_OS_STD_LIB)

PLATFORM_LDFLAGS += -lcurl

#PLATFORM_LDFLAGS += -arch i386
# PLATFORM_LDFLAGS += -lcurl
PLATFORM_LDFLAGS += -mmacosx-version-min=$(MAC_OS_MIN_VERSION) -v

##########################################################################################
Expand Down Expand Up @@ -334,19 +340,24 @@ PLATFORM_LIBRARY_SEARCH_PATHS =
PLATFORM_FRAMEWORKS =
PLATFORM_FRAMEWORKS += Accelerate
PLATFORM_FRAMEWORKS += AGL
PLATFORM_FRAMEWORKS += AppKit
PLATFORM_FRAMEWORKS += ApplicationServices
PLATFORM_FRAMEWORKS += AVFoundation
PLATFORM_FRAMEWORKS += AudioToolbox
PLATFORM_FRAMEWORKS += Cocoa
PLATFORM_FRAMEWORKS += CoreVideo
PLATFORM_FRAMEWORKS += CoreAudio
PLATFORM_FRAMEWORKS += CoreMedia
PLATFORM_FRAMEWORKS += CoreFoundation
PLATFORM_FRAMEWORKS += CoreServices
PLATFORM_FRAMEWORKS += OpenGL
PLATFORM_FRAMEWORKS += Metal
PLATFORM_FRAMEWORKS += Foundation
PLATFORM_FRAMEWORKS += IOKit
PLATFORM_FRAMEWORKS += Cocoa
PLATFORM_FRAMEWORKS += CoreVideo
PLATFORM_FRAMEWORKS += AVFoundation
PLATFORM_FRAMEWORKS += CoreMedia
PLATFORM_FRAMEWORKS += OpenGL
PLATFORM_FRAMEWORKS += QuartzCore
PLATFORM_FRAMEWORKS += Security
PLATFORM_FRAMEWORKS += SystemConfiguration


ifeq ($(USE_GST),1)
PLATFORM_FRAMEWORKS += GStreamer
Expand Down Expand Up @@ -399,23 +410,23 @@ afterplatform: $(TARGET_NAME)
@mkdir -p bin/$(BIN_NAME).app/Contents/Resources

# Use the openFrameworks-Info.plist as the default. Feel free to edit it in your project folder to override and values.
@if [ ! -f openFrameworks-Info.plist ]; then cp $(OF_ROOT)/scripts/templates/macos/openFrameworks-Info.plist openFrameworks-Info.plist; fi
@if [ ! -f openFrameworks-Info.plist ]; then cp $(OF_ROOT)/scripts/templates/osx/openFrameworks-Info.plist openFrameworks-Info.plist; fi
@cp openFrameworks-Info.plist bin/$(BIN_NAME).app/Contents/Info.plist;

# App icons
ifeq ($(RUN_TARGET), RunRelease)
@if [ -f of.icns ]; then cp of.icns bin/$(BIN_NAME).app/Contents/Resources/; else cp $(OF_LIBS_PATH)/openFrameworksCompiled/project/macos/of.icns bin/$(BIN_NAME).app/Contents/Resources/; fi
@sed -i '' 's/\$$(ICON_NAME)/of.icns/g' bin/$(BIN_NAME).app/Contents/Info.plist
@if [ -f of.icns ]; then cp of.icns bin/$(BIN_NAME).app/Contents/Resources/; else cp $(OF_LIBS_PATH)/openFrameworksCompiled/project/osx/of.icns bin/$(BIN_NAME).app/Contents/Resources/; fi
@sed -i '' 's/\$${ICON_NAME}/of.icns/g' bin/$(BIN_NAME).app/Contents/Info.plist
else
@if [ -f of_debug.icns ]; then cp of_debug.icns bin/$(BIN_NAME).app/Contents/Resources/; else cp $(OF_LIBS_PATH)/openFrameworksCompiled/project/macos/of_debug.icns bin/$(BIN_NAME).app/Contents/Resources/; fi
@sed -i '' 's/\$$(ICON_NAME)/of_debug.icns/g' bin/$(BIN_NAME).app/Contents/Info.plist
@if [ -f of_debug.icns ]; then cp of_debug.icns bin/$(BIN_NAME).app/Contents/Resources/; else cp $(OF_LIBS_PATH)/openFrameworksCompiled/project/osx/of_debug.icns bin/$(BIN_NAME).app/Contents/Resources/; fi
@sed -i '' 's/\$${ICON_NAME}/of_debug.icns/g' bin/$(BIN_NAME).app/Contents/Info.plist
endif

@sed -i '' 's/\$$(DEVELOPMENT_LANGUAGE)/English/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$$(EXECUTABLE_NAME)/$(BIN_NAME)/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$$(TARGET_NAME)/$(BIN_NAME)/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$$(PRODUCT_BUNDLE_IDENTIFIER)/cc.openFrameworks.$(BIN_NAME)/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$$(VERSION)/1.0/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$${DEVELOPMENT_LANGUAGE}/English/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$${EXECUTABLE_NAME}/$(BIN_NAME)/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$${TARGET_NAME}/$(BIN_NAME)/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$${PRODUCT_BUNDLE_IDENTIFIER}/cc.openFrameworks.$(BIN_NAME)/g' bin/$(BIN_NAME).app/Contents/Info.plist
@sed -i '' 's/\$${MARKETING_VERSION}/1.0/g' bin/$(BIN_NAME).app/Contents/Info.plist

@echo TARGET=$(TARGET)
@mv $(TARGET) bin/$(BIN_NAME).app/Contents/MacOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ ifndef PLATFORM_LIB_SUBPATH
PLATFORM_LIB_SUBPATH=android
SHARED_LIB_EXTENSION=so
else ifeq ($(PLATFORM_OS),Darwin)
PLATFORM_LIB_SUBPATH=osx
PLATFORM_LIB_SUBPATH=macos
SHARED_LIB_EXTENSION=dylib
else ifeq ($(PLATFORM_OS),emscripten)
PLATFORM_LIB_SUBPATH=emscripten
Expand Down
8 changes: 4 additions & 4 deletions libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ HEADER_URIPARSER = "$(OF_PATH)/libs/uriparser/include"
HEADER_PUGIXML = "$(OF_PATH)/libs/pugixml/include"

//------- Libraries
LIB_OF = "$(OF_PATH)/libs/openFrameworksCompiled/lib/osx/openFrameworks.a"
LIB_OF_DEBUG = "$(OF_PATH)/libs/openFrameworksCompiled/lib/osx/openFrameworksDebug.a"
LIB_OF = "$(OF_PATH)/libs/openFrameworksCompiled/lib/macos/openFrameworks.a"
LIB_OF_DEBUG = "$(OF_PATH)/libs/openFrameworksCompiled/lib/macos/openFrameworksDebug.a"

LIB_FMOD = "$(OF_PATH)/libs/fmod/lib/osx/libfmod.dylib"
LIB_FMOD = "$(OF_PATH)/libs/fmod/lib/macos/libfmod.dylib"

//LIB_GLFW = "$(OF_PATH)/libs/glfw/lib/osx/glfw3.a"
//LIB_FREEIMAGE = "$(OF_PATH)/libs/FreeImage/lib/osx/freeimage.a"
Expand All @@ -44,7 +44,7 @@ LIB_FMOD = "$(OF_PATH)/libs/fmod/lib/osx/libfmod.dylib"
//LIB_URIPARSER = "$(OF_PATH)/libs/uriparser/lib/osx/uriparser.a"
//LIB_PUGIXML = "$(OF_PATH)/libs/pugixml/lib/osx/pugixml.a"

OF_CORE_LIBS = $(LIB_FMOD)
OF_CORE_LIBS = $(LIB_FMOD) //$(LIB_CURL)

OF_CORE_HEADERS = $(HEADER_OF) $(HEADER_FREETYPE) $(HEADER_FREETYPE2) $(HEADER_FMOD) $(HEADER_GLEW) $(HEADER_FREEIMAGE) $(HEADER_TESS2) $(HEADER_CAIRO) $(HEADER_RTAUDIO) $(HEADER_GLFW) $(HEADER_UTF8) $(HEADER_JSON) $(HEADER_GLM) $(HEADER_CURL) ${HEADER_SSL} $(HEADER_URIPARSER) $(HEADER_PUGIXML) ${HEADER_BROTLI}

Expand Down
10 changes: 8 additions & 2 deletions libs/openFrameworksCompiled/project/osx/config.osx.default.mk
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ ifndef MAC_OS_STD_LIB
MAC_OS_STD_LIB = libc++
endif

ifndef MAC_OS_C_VER
MAC_OS_C_VER = -std=c17
endif

ifndef MAC_OS_CPP_VER
MAC_OS_CPP_VER = -std=c++17
MAC_OS_CPP_VER = -std=c++23
endif

# Link against libstdc++ to silence tr1/memory errors on latest versions of osx
Expand Down Expand Up @@ -180,7 +184,9 @@ ifdef MAC_OS_SDK_ROOT
endif

PLATFORM_CFLAGS += -mmacosx-version-min=$(MAC_OS_MIN_VERSION)
PLATFORM_CFLAGS += $(MAC_OS_C_VER)

PLATFORM_CXXFLAGS += -mmacosx-version-min=$(MAC_OS_MIN_VERSION)
PLATFORM_CXXFLAGS += -x objective-c++
PLATFORM_CXXFLAGS += $(MAC_OS_CPP_VER)

Expand All @@ -203,7 +209,7 @@ endif

PLATFORM_LDFLAGS = -stdlib=$(MAC_OS_STD_LIB)
#PLATFORM_LDFLAGS += -arch i386
PLATFORM_LDFLAGS += -lcurl
# PLATFORM_LDFLAGS += -lcurl
PLATFORM_LDFLAGS += -mmacosx-version-min=$(MAC_OS_MIN_VERSION) -v

##########################################################################################
Expand Down
Loading

0 comments on commit 252297e

Please sign in to comment.