Skip to content

Commit

Permalink
[Closes #8] Add support for Spin2Cpp via function and add_custom_command
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidZemon committed Feb 13, 2016
1 parent 6005fc7 commit 3370d5b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
47 changes: 45 additions & 2 deletions CMakeModules/FindPropWare.cmake
Expand Up @@ -87,8 +87,7 @@ if (NOT PropWare_FOUND)
endforeach()
else ()
if (NOT DEFINED PROPWARE_PATH
OR NOT EXISTS "${PROPWARE_PATH}/include/PropWare/PropWare.h"
OR NOT EXISTS "${PROPWARE_PATH}/CMakePropWareInstall.cmake")
OR NOT EXISTS "${PROPWARE_PATH}/lib/PropWare-targets.cmake")

find_path(PROPWARE_PATH
NAMES
Expand Down Expand Up @@ -418,6 +417,50 @@ if (NOT PropWare_FOUND)
set_compile_flags()
set_linker(${name})
endmacro()

function(spin2cpp source output_var_name)
get_filename_component(SOURCE_PATH "${source}" ABSOLUTE)

# Find output files
execute_process(COMMAND "${SPIN2CPP_COMMAND}" --files "${SOURCE_PATH}"
OUTPUT_VARIABLE FILES_STRING
RESULT_VARIABLE SPIN2CPP_DEPENDS_CODE
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (SPIN2CPP_DEPENDS_CODE)
message(FATAL_ERROR "Spin2cpp failed to report dependencies. Exit code ${SPIN2CPP_DEPENDS_CODE}")
endif ()

# Convert output files from newline-separated list to CMake list
string(REPLACE "\r" "" OUTPUT_FILE_NAMES "${FILES_STRING}")
string(REPLACE "\n" ";" OUTPUT_FILE_NAMES "${OUTPUT_FILE_NAMES}")
foreach (file_name IN LISTS OUTPUT_FILE_NAMES)
list(APPEND ALL_OUTPUT_FILES "${CMAKE_CURRENT_BINARY_DIR}/${file_name}")
endforeach ()

# Only save new files in the "output list" variable and add to the clean target
foreach (file_path IN LISTS ALL_OUTPUT_FILES)
list(FIND FILES_GENERATED_IN_DIRECTORY ${file_path} INDEX)
if ("-1" STREQUAL INDEX)
list(APPEND FILES_GENERATED_IN_DIRECTORY "${file_path}")
list(APPEND UNIQUE_OUTPUT_FILES "${file_path}")
endif ()
endforeach ()
set(FILES_GENERATED_IN_DIRECTORY ${FILES_GENERATED_IN_DIRECTORY} PARENT_SCOPE)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${FILES_GENERATED_IN_DIRECTORY}")

if (UNIQUE_OUTPUT_FILES)
if (ARGN)
set(MAIN_FLAG "--main")
endif ()
add_custom_command(OUTPUT ${UNIQUE_OUTPUT_FILES}
COMMAND "${SPIN2CPP_COMMAND}"
ARGS --gas ${MAIN_FLAG} "${SOURCE_PATH}"
MAIN_DEPENDENCY "${SOURCE_PATH}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Converting ${source} to C++")
endif ()
set(${output_var_name} ${UNIQUE_OUTPUT_FILES} PARENT_SCOPE)
endfunction()
endif ()

# TODO: Add build system documentation for testing
Expand Down
1 change: 1 addition & 0 deletions Examples/CMakeLists.txt
Expand Up @@ -34,3 +34,4 @@ add_subdirectory(Simple_FdSerial)
add_subdirectory(Simple_libadcACpropab)
add_subdirectory(Simple_MeasureVolts)
add_subdirectory(Simple_SimpleText)
add_subdirectory(Spin2Cpp)
15 changes: 15 additions & 0 deletions Examples/Spin2Cpp/CMakeLists.txt
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.3)

# Spin2Cpp is a PropWare 2.1 veature, so you must have at least PropWare 2.1
find_package(PropWare 2.1 REQUIRED)

project(Spin2Cpp_Demo)

# FullDuplexSerial.spin is just a library, so we only pass the name of the Spin file and a name for the output variable
spin2cpp(FullDuplexSerial.spin FullDuplexSerial_CXX_FILES)

# Hello.spin contains our "main" method, so we need to pass a third parameter which marks it as the main file
spin2cpp(Hello.spin Hello_CXX_FILES 1)

# Now we create an executable and include source files that match up to our spin files.
create_simple_executable(Spin2Cpp_Demo ${Hello_CXX_FILES} ${FullDuplexSerial_CXX_FILES})
Binary file added Examples/Spin2Cpp/FullDuplexSerial.spin
Binary file not shown.
16 changes: 16 additions & 0 deletions Examples/Spin2Cpp/Hello.spin
@@ -0,0 +1,16 @@
CON
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000

OBJ
fds : "FullDuplexSerial.spin"

PUB demo | i

'' start up the serial port
fds.start(31, 30, 0, 115200)

'' and say hello
repeat 3
fds.str(string("Hello, world!", 13, 10))

0 comments on commit 3370d5b

Please sign in to comment.