Skip to content

Commit 87d95af

Browse files
committed
Rewrite unix2dos
Don't depend on external tools. Fix running on Windows.
1 parent a2d5ce3 commit 87d95af

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

cmake/licensing_helper.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ set(LICENSING_OPTIONAL_ITEMS "" CACHE STRING
3636
"List of items which may be deployed even when licensing documentation is absent"
3737
)
3838

39-
# Import unix2dos configuration
40-
include("unix2dos")
41-
4239

4340
# Deploy 3rd-party copyright, terms etc.
4441
# The app name can be followed by the filenames of its explicit license texts as

cmake/licensing_helper_install.cmake.in

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ macro(HANDLE_CROSSCOMPILING)
3131
endmacro()
3232

3333

34+
function(LICENSING_HELPER_UNIX2DOS)
35+
handle_crosscompiling()
36+
include("unix2dos")
37+
unix2dos(${ARGN})
38+
endfunction()
39+
40+
3441
function(LICENSING_HELPER_COLLECT_RUNTIME var)
3542
handle_crosscompiling()
3643
if(MINGW)
@@ -86,9 +93,7 @@ endfunction()
8693
function(LICENSING_HELPER_DEPLOY_COPYRIGHT app)
8794
set(licensing_dir "$ENV{DESTDIR}${CMAKE_INSTALL_FULL_DOCDIR}/licensing")
8895
file(INSTALL ${ARGN} DESTINATION "${licensing_dir}")
89-
handle_crosscompiling()
90-
include("unix2dos")
91-
unix2dos("${licensing_dir}/*")
96+
licensing_helper_unix2dos("${licensing_dir}/*")
9297
endfunction()
9398

9499

@@ -114,9 +119,7 @@ endfunction()
114119
function(LICENSING_HELPER_DEPLOY_COMMON app)
115120
set(common_dir "$ENV{DESTDIR}${CMAKE_INSTALL_FULL_DOCDIR}/licensing/common-licenses")
116121
file(INSTALL ${ARGN} DESTINATION "${common_dir}")
117-
handle_crosscompiling()
118-
include("unix2dos")
119-
unix2dos("${common_dir}/*")
122+
licensing_helper_unix2dos("${common_dir}/*")
120123
endfunction()
121124

122125

@@ -143,17 +146,15 @@ function(LICENSING_HELPER_DEDUP_COMMON app)
143146
endfunction()
144147

145148

149+
list(APPEND CMAKE_MODULE_PATH "@PROJECT_SOURCE_DIR@/cmake")
150+
146151
set(BUNDLE_EXECUTABLE "@BUNDLE_EXECUTABLE@")
147152
set(BUNDLE_COPYRIGHT "@BUNDLE_COPYRIGHT@")
148153

149154
set(LICENSING_OPTIONAL_ITEMS "@LICENSING_OPTIONAL_ITEMS@")
150155
set(LICENSING_COPYRIGHT_DIR "@LICENSING_COPYRIGHT_DIR@")
151156
set(LICENSING_COMMON_DIR "@LICENSING_COMMON_DIR@")
152157

153-
set(UNIX2DOS_COMMAND "@UNIX2DOS_COMMAND@")
154-
set(UNIX2DOS_SED_COMMAND "@UNIX2DOS_SED_COMMAND@")
155-
list(APPEND CMAKE_MODULE_PATH "@PROJECT_SOURCE_DIR@/cmake")
156-
157158
set(CMAKE_INSTALL_BINDIR "@CMAKE_INSTALL_BINDIR@")
158159
set(CMAKE_INSTALL_LIBDIR "@CMAKE_INSTALL_LIBDIR@")
159160
set(CMAKE_INSTALL_DOCDIR "@CMAKE_INSTALL_DOCDIR@")

cmake/unix2dos.cmake

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@
1717
# along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
1818

1919

20+
# Script mode
21+
if(UNIX2DOS_FILE)
22+
file(READ "${UNIX2DOS_FILE}" content)
23+
string(REGEX REPLACE "\r*\n" "\r\n" content "${content}")
24+
file(WRITE "${UNIX2DOS_FILE}" "${content}")
25+
return()
26+
endif()
27+
28+
29+
set(UNIX2DOS_ENABLED "${WIN32}" CACHE BOOL "Convert line endings for Windows")
30+
31+
2032
if(COMMAND unix2dos)
2133
# include guard
2234
return()
23-
elseif(NOT WIN32)
24-
# no-op if not building for Windows
35+
elseif(NOT UNIX2DOS_ENABLED)
2536
function(UNIX2DOS)
2637
endfunction()
2738
function(UNIX2DOS_INSTALLED)
@@ -30,48 +41,42 @@ elseif(NOT WIN32)
3041
endif()
3142

3243

33-
message(STATUS "Enabling unix2dos")
44+
# Windows-only
45+
46+
set(UNIX2DOS_LIST_FILE "${CMAKE_CURRENT_LIST_FILE}")
3447

35-
find_program(UNIX2DOS_COMMAND
36-
NAMES unix2dos
37-
DOC "Filepath of the unix2dos executable"
38-
)
39-
find_program(SED_COMMAND
40-
NAMES gsed sed
41-
DOC "Filepath of the sed executable"
42-
)
43-
if(NOT UNIX2DOS_COMMAND AND NOT SED_COMMAND)
44-
message(WARNING "unix2dos or sed are required to convert text files for Windows")
45-
endif()
46-
mark_as_advanced(UNIX2DOS_COMMAND)
47-
mark_as_advanced(UNIX2DOS_SED_COMMAND)
4848

49+
# Convert the files matching the given pattern to Windows line endings.
50+
# Cf. CMake's file(GLOB ...) for the pattern syntax
4951

50-
# On Windows, convert the files matching the given pattern from UNIX to Windows
51-
# line endings. Cf. CMake's file(GLOB ...) for the pattern syntax
5252
function(UNIX2DOS)
53-
file(GLOB files ${ARGN} LIST_DIRECTORIES false)
53+
file(GLOB files LIST_DIRECTORIES false ${ARGN})
5454
foreach(file ${files})
55-
if(UNIX2DOS_COMMAND)
56-
execute_process(COMMAND "${UNIX2DOS_COMMAND}" -ascii --quiet "${file}")
57-
elseif(UNIX2DOS_SED_COMMAND)
58-
execute_process(
59-
COMMAND "${UNIX2DOS_SED_COMMAND}" -e "s,\\r*$,\\r," -i -- "${file}"
60-
COMMAND "${CMAKE_COMMAND}" -E remove -f "${file}--"
61-
)
55+
execute_process(
56+
COMMAND "${CMAKE_COMMAND}" "-DUNIX2DOS_FILE=${file}" -P "${UNIX2DOS_LIST_FILE}"
57+
RESULT_VARIABLE result
58+
ERROR_VARIABLE error
59+
)
60+
if(NOT result EQUAL 0)
61+
message(FATAL_ERROR "${error}")
6262
endif()
6363
endforeach()
6464
endfunction()
6565

6666

67+
# At installation time, convert the files matching the given installation path
68+
# pattern from UNIX to Windows line endings.
69+
# Cf. CMake's file(GLOB ...) for the pattern syntax
70+
6771
function(UNIX2DOS_INSTALLED)
6872
set(code
69-
"list(APPEND CMAKE_MODULE_PATH \"${PROJECT_SOURCE_DIR}/cmake\")"
70-
"include(\"unix2dos\")"
73+
"set(UNIX2DOS_ENABLED ON CACHE BOOL \"Convert line endings for Windows\")"
74+
"include(\"${UNIX2DOS_LIST_FILE}\")"
7175
)
7276
foreach(pattern ${ARGN})
7377
list(APPEND code "unix2dos(\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${pattern}\")")
7478
endforeach()
7579
string(REPLACE ";" "\n " code "${code}")
7680
install(CODE "${code}")
7781
endfunction()
82+

0 commit comments

Comments
 (0)