Skip to content

Commit

Permalink
Begin the structured tracking and application of patches to vendored …
Browse files Browse the repository at this point in the history
…code (#720)

* Move existing libxls-ingesting-and-patching script to maintenance/

* Re-vendor libxls files

* Create patch file re: filepath changes

* git apply maintenance/patches/filepaths.patch

* This is not an R file

* Create patch around the masking in cran.h

* git apply maintenance/patches/cran.patch

* Create the patch around locale

* git apply maintenance/patches/locale.patch
  • Loading branch information
jennybc authored Feb 7, 2023
1 parent 00b0209 commit 9104e69
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 1 deletion.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
^\.vscode$
^debug$
^CRAN-SUBMISSION$
^maintenance$
File renamed without changes.
36 changes: 36 additions & 0 deletions maintenance/02_apply-our-libxls-patches.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# https://stackoverflow.com/questions/6336440/how-can-i-make-git-am-git-apply-work-fuzzy-like-the-patch-command
# https://www.thegeekstuff.com/2014/12/patch-command-examples/

# files that need a patch only related to filepaths
# src/libxls/brdb.h
# src/libxls/endian.c
# src/libxls/endian.h
# src/libxls/ole.c
# src/libxls/ole.h
# src/libxls/xls.c
# src/libxls/xlsstruct.h
# src/libxls/xlstool.c
# src/libxls/xlstool.h

# file that has a patch related to filepaths (but also other patches)
# src/libxls/locale.c

# I implemented the filepath changes (and only the filepath changes) and did:
# git diff ':!maintenance/02_apply-our-libxls-patches.txt' > maintenance/patches/filepaths.patch
# https://stackoverflow.com/questions/10415100/exclude-file-from-git-diff

# undo those changes

# apply the changes by applying a patch
# git apply maintenance/patches/filepaths.patch

# Implement the change where we mask functions cran doesn't like in this file:
# src/libxls/xlstypes.h
# then make the .patch file
# git diff ':!maintenance/02_apply-our-libxls-patches.txt' > maintenance/patches/cran.patch

# Implement the locale related changes in
# src/libxls/locale.c
# src/libxls/locale.h
# then make the .patch file
# git diff ':!maintenance/02_apply-our-libxls-patches.txt' > maintenance/patches/locale.patch
15 changes: 15 additions & 0 deletions maintenance/patches/cran.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/src/libxls/xlstypes.h b/src/libxls/xlstypes.h
index 52da772..fb68147 100644
--- a/src/libxls/xlstypes.h
+++ b/src/libxls/xlstypes.h
@@ -36,6 +36,10 @@
#ifndef XLS_TYPES_INC
#define XLS_TYPES_INC

+/* Mask illegal functions for CMD check */
+/* an analysis of header file includes shows that doing this here covers everything */
+#include "cran.h"
+
#ifdef __cplusplus
#include <cstdint>
#else
152 changes: 152 additions & 0 deletions maintenance/patches/filepaths.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
diff --git a/src/libxls/brdb.h b/src/libxls/brdb.h
index a84b7d6..08698a2 100644
--- a/src/libxls/brdb.h
+++ b/src/libxls/brdb.h
@@ -43,7 +43,7 @@ typedef struct str_brdb record_brdb;

record_brdb brdb[] =
{
-#include "../libxls/brdb.c.h"
+#include "libxls/brdb.c.h"
};

static int get_brbdnum(int id)
diff --git a/src/libxls/endian.c b/src/libxls/endian.c
index 1b5032f..f126936 100644
--- a/src/libxls/endian.c
+++ b/src/libxls/endian.c
@@ -31,9 +31,9 @@

#include <stdlib.h>

-#include "../include/libxls/xlstypes.h"
-#include "../include/libxls/endian.h"
-#include "../include/libxls/ole.h"
+#include "libxls/xlstypes.h"
+#include "libxls/endian.h"
+#include "libxls/ole.h"

int xls_is_bigendian()
{
diff --git a/src/libxls/endian.h b/src/libxls/endian.h
index 68a7c75..8315da8 100644
--- a/src/libxls/endian.h
+++ b/src/libxls/endian.h
@@ -29,7 +29,7 @@
*
*/

-#include "../libxls/xlsstruct.h"
+#include "libxls/xlsstruct.h"

int xls_is_bigendian(void);
DWORD xlsIntVal (DWORD i);
diff --git a/src/libxls/locale.c b/src/libxls/locale.c
index 65b5b28..f8fc4a9 100644
--- a/src/libxls/locale.c
+++ b/src/libxls/locale.c
@@ -30,7 +30,7 @@
*/
#include "config.h"
#include <stdlib.h>
-#include "../include/libxls/locale.h"
+#include "libxls/locale.h"

xls_locale_t xls_createlocale() {
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
diff --git a/src/libxls/ole.c b/src/libxls/ole.c
index 082ac2a..90527ee 100644
--- a/src/libxls/ole.c
+++ b/src/libxls/ole.c
@@ -40,9 +40,9 @@
#include <stdio.h>
#include <stdlib.h>

-#include "../include/libxls/ole.h"
-#include "../include/libxls/xlstool.h"
-#include "../include/libxls/endian.h"
+#include "libxls/ole.h"
+#include "libxls/xlstool.h"
+#include "libxls/endian.h"

extern int xls_debug;

diff --git a/src/libxls/ole.h b/src/libxls/ole.h
index 09e802e..b376299 100644
--- a/src/libxls/ole.h
+++ b/src/libxls/ole.h
@@ -42,7 +42,7 @@
#include <stdio.h> // FILE *
#endif

-#include "../libxls/xlstypes.h"
+#include "libxls/xlstypes.h"

#if defined(_AIX) || defined(__sun)
#pragma pack(1)
diff --git a/src/libxls/xls.c b/src/libxls/xls.c
index fcbcacc..cf17895 100644
--- a/src/libxls/xls.c
+++ b/src/libxls/xls.c
@@ -49,9 +49,9 @@
#include <string.h>
#include <wchar.h>

-#include "../include/libxls/endian.h"
-#include "../include/libxls/locale.h"
-#include "../include/xls.h"
+#include "libxls/endian.h"
+#include "libxls/locale.h"
+#include "libxls/xls.h"

#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
diff --git a/src/libxls/xlsstruct.h b/src/libxls/xlsstruct.h
index cfa3a96..9878506 100644
--- a/src/libxls/xlsstruct.h
+++ b/src/libxls/xlsstruct.h
@@ -36,7 +36,7 @@
#ifndef XLS_STRUCT_INC
#define XLS_STRUCT_INC

-#include "../libxls/ole.h"
+#include "libxls/ole.h"

#define XLS_RECORD_EOF 0x000A
#define XLS_RECORD_DEFINEDNAME 0x0018
diff --git a/src/libxls/xlstool.c b/src/libxls/xlstool.c
index 14df59a..1bede81 100644
--- a/src/libxls/xlstool.c
+++ b/src/libxls/xlstool.c
@@ -50,12 +50,12 @@
#include <string.h>

//#include "xls.h"
-#include "../include/libxls/xlstypes.h"
-#include "../include/libxls/xlsstruct.h"
-#include "../include/libxls/xlstool.h"
-#include "../include/libxls/brdb.h"
-#include "../include/libxls/endian.h"
-#include "../include/libxls/locale.h"
+#include "libxls/xlstypes.h"
+#include "libxls/xlsstruct.h"
+#include "libxls/xlstool.h"
+#include "libxls/brdb.h"
+#include "libxls/endian.h"
+#include "libxls/locale.h"

extern int xls_debug;

diff --git a/src/libxls/xlstool.h b/src/libxls/xlstool.h
index 5c91529..ae1f04c 100644
--- a/src/libxls/xlstool.h
+++ b/src/libxls/xlstool.h
@@ -33,7 +33,7 @@
*
*/

-#include "../libxls/xlsstruct.h"
+#include "libxls/xlsstruct.h"

void verbose(char* str);

70 changes: 70 additions & 0 deletions maintenance/patches/locale.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
diff --git a/src/libxls/locale.c b/src/libxls/locale.c
index f8fc4a9..8f8eb35 100644
--- a/src/libxls/locale.c
+++ b/src/libxls/locale.c
@@ -32,8 +32,26 @@
#include <stdlib.h>
#include "libxls/locale.h"

-xls_locale_t xls_createlocale() {
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
+#ifdef __GNUC__
+ // Rtools42 | R 4.2 | GCC 10 | UCRT
+ // Rtools40 | R 4.0.x to 4.1.x | GCC 8.3.0 | MSVCRT
+ // Rtools35 | R 3.3.x to 3.6.x | GCC 4.9.3 | MSVCRT
+ #if __GNUC__ < 10
+ #define WINDOWS_BEFORE_RTOOLS_42
+ #endif
+#endif
+#endif
+
+#ifdef WINDOWS_BEFORE_RTOOLS_42
+ static char* old_locale;
+#endif
+
+xls_locale_t xls_createlocale() {
+#if defined(WINDOWS_BEFORE_RTOOLS_42)
+ old_locale = setlocale(LC_CTYPE, ".65001");
+ return NULL;
+#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
return _create_locale(LC_CTYPE, ".65001");
#else
return newlocale(LC_CTYPE_MASK, "C.UTF-8", NULL);
@@ -41,6 +59,11 @@ xls_locale_t xls_createlocale() {
}

void xls_freelocale(xls_locale_t locale) {
+#if defined(WINDOWS_BEFORE_RTOOLS_42)
+ setlocale(LC_CTYPE, old_locale);
+ return;
+#endif
+
if (!locale)
return;
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
@@ -51,7 +74,9 @@ void xls_freelocale(xls_locale_t locale) {
}

size_t xls_wcstombs_l(char *restrict s, const wchar_t *restrict pwcs, size_t n, xls_locale_t loc) {
-#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
+#if defined(WINDOWS_BEFORE_RTOOLS_42)
+ return wcstombs(s, pwcs, n);
+#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
return _wcstombs_l(s, pwcs, n, loc);
#elif defined(HAVE_WCSTOMBS_L)
return wcstombs_l(s, pwcs, n, loc);
diff --git a/src/libxls/locale.h b/src/libxls/locale.h
index 3ae3911..b047739 100644
--- a/src/libxls/locale.h
+++ b/src/libxls/locale.h
@@ -35,6 +35,10 @@

#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
typedef _locale_t xls_locale_t;
+#elif defined(__sun)
+/* we never actually use the only variable declared with this type on solaris,
+ * so its precise type doesn't really matter */
+typedef void* xls_locale_t;
#else
typedef locale_t xls_locale_t;
#endif
2 changes: 1 addition & 1 deletion src/libxls/xlstypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#define XLS_TYPES_INC

/* Mask illegal functions for CMD check */
/* an analysis header file includes shows that doing this here covers everything */
/* an analysis of header file includes shows that doing this here covers everything */
#include "cran.h"

#ifdef __cplusplus
Expand Down

0 comments on commit 9104e69

Please sign in to comment.