examples: C/C++ examples use transitive dependencies

Updated the examples to comprehend transitive dependencies.  This means
that each example target will no longer have a giant list of -I includes
(the examples at the end of the list had includes for all previous
examples, upwards of 200 -I's on the command line).

    * Created a CMakeLists.txt in the upm/examples directory, moved
      common functionality to this level.
    * C/C++ examples now look to the filename for their dependency
      target name, ie; gas-mq2.cxx adds a dependency to the 'gas' target
    * Updated a handful of C/C++ example names to reflect this
    * Example CMake flow - glob the list of files, add targets for any
      special case examples, then att targets for all the rest

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2017-04-03 14:39:11 -07:00
parent 76a12af15f
commit 048f1ac08e
57 changed files with 160 additions and 600 deletions

View File

@ -1,172 +1,11 @@
# Extract module name from non-standard example name
macro(get_module_name example_name module_name)
string(LENGTH ${example_name} length)
string(FIND ${example_name} "-" index)
if (${index} GREATER 1)
string(SUBSTRING ${example_name} 0 ${index} substr)
set(${module_name} ${substr})
elseif (${example_name} MATCHES "^grove")
set (${module_name} "grove")
elseif ((${example_name} MATCHES "^mq" AND ${length} EQUAL 3) OR ${example_name} STREQUAL "tp401")
set (${module_name} "gas")
else()
set(${module_name} ${example_name})
endif()
endmacro()
# Create an list of all C source files in this directory
file (GLOB example_src_list RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.c")
# Set source file, include and linker settings for an example
# If example cannot be built, example_bin is cleared
macro(add_custom_example example_bin example_src example_module_list)
set(found_all_modules TRUE)
foreach (module ${example_module_list})
if (NOT EXISTS "${PROJECT_SOURCE_DIR}/src/${module}")
set(found_all_modules FALSE)
endif()
if (MODULE_LIST)
list(FIND MODULE_LIST ${module} index)
if (${index} EQUAL -1)
set(found_all_modules FALSE)
endif()
endif()
endforeach()
if (found_all_modules)
add_executable (${example_bin} ${example_src})
target_link_libraries (${example_bin} ${CMAKE_THREAD_LIBS_INIT})
foreach (module ${example_module_list})
set(module_dir "${PROJECT_SOURCE_DIR}/src/${module}")
include_directories (${module_dir})
if (${module} STREQUAL "lcd")
set(module "i2clcd")
endif()
target_link_libraries (${example_bin} ${module}-c)
endforeach()
else()
message (WARNING "Ignored ${example_bin}")
set (example_bin "")
endif()
endmacro()
# - Handle special cases here --------------------------------------------------
#add_example(humidity-sensor.c TARGETS si7005-c bmp280-c)
# Add specified example by name
# Note special case for grove based examples
macro(add_example example_name)
set(example_src "${example_name}.c")
set(example_bin "${example_name}-example-c")
get_module_name(${example_name} module_name)
set(module_dir "${PROJECT_SOURCE_DIR}/src/${module_name}")
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${example_src}"
AND EXISTS ${module_dir}
AND IS_DIRECTORY ${module_dir})
add_custom_example(${example_bin} ${example_src} ${module_name})
if ((NOT ${example_bin} STREQUAL "") AND (${module_name} STREQUAL "grove"))
set(grove_module_path "${PROJECT_SOURCE_DIR}/src/${example_name}")
if (EXISTS ${grove_module_path})
include_directories(${grove_module_path})
target_link_libraries (${example_bin} ${example_name})
endif()
endif()
else()
message (WARNING "Ignored ${example_bin}")
endif()
endmacro()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples)
# UPM c include directories
include_directories (${PROJECT_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src/utilities)
# Set the mraa include and link directories prior to adding examples
include_directories (${MRAA_INCLUDE_DIRS})
link_directories (${MRAA_LIBDIR})
# If your sample source file matches the name of the module it tests, add it here
# Exceptions are as follows:
# string after first '-' is ignored (e.g. nrf24l01-transmitter maps to nrf24l01)
# mq? will use module gas
# grove* will use module grove
add_example (dfrph)
add_example (nmea_gps)
add_example (mma7361)
add_example (bh1750)
add_example (urm37)
add_example (urm37-uart)
add_example (hka5)
add_example (dfrorp)
add_example (vdiv)
add_example (mqx)
add_example (o2)
add_example (emg)
add_example (gsr)
add_example (light)
add_example (ldt0028)
add_example (joystick12)
add_example (flex)
add_example (slide)
add_example (mq303a)
add_example (m24lr64e)
add_example (mpr121)
add_example (servo)
add_example (a110x)
add_example (gp2y0a)
add_example (ttp223)
add_example (loudness)
add_example (tsl2561)
add_example (collision)
add_example (moisture)
add_example (led)
add_example (ds18b20)
add_example (dfrec)
add_example (sht1x)
add_example (water)
add_example (yg1006)
add_example (biss0001)
add_example (bmi160)
add_example (jhd1313m1)
add_example (lm35)
add_example (rotaryencoder)
add_example (rpr220)
add_example (md)
add_example (linefinder)
add_example (uln200xa)
add_example (mma7660)
add_example (buzzer)
add_example (ppd42ns)
add_example (guvas12d)
add_example (otp538u)
add_example (button)
add_example (my9221)
add_example (ms5803)
add_example (ims)
add_example (ecezo)
add_example (mb704x)
add_example (mcp2515)
add_example (max30100)
add_example (speaker)
add_example (cjq4435)
add_example (hmc5883l)
add_example (wfs)
add_example (enc03r)
add_example (nunchuck)
add_example (bno055)
add_example (bmp280)
add_example (abp)
add_example (lcdks)
add_example (bmg160)
add_example (bma250e)
add_example (bmm150)
add_example (rsc)
add_example (bmpx8x)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
add_custom_example (lcm1602-i2c-example-c lcm1602-i2c.c lcm1602)
add_custom_example (lcm1602-parallel-example-c lcm1602-parallel.c lcm1602)
add_custom_example (rpr220-intr-example-c rpr220-intr.c rpr220)
add_custom_example (md-stepper-example-c md-stepper.c md)
add_custom_example (button_intr-example-c button_intr.c button)
add_custom_example (mcp2515-txrx-example-c mcp2515-txrx.c mcp2515)
add_custom_example (le910-example-c le910.c uartat)
add_custom_example (speaker_pwm-example-c speaker_pwm.c speaker)
add_custom_example (bme280-example-c bme280.c bmp280)
# - Create an executable for all other src files in this directory -------------
foreach (_example_src ${example_src_list})
add_example(${_example_src} SUFFIX "-c")
endforeach ()

View File

@ -1,27 +0,0 @@
//Modified: Abhishek Malik <abhishek.malik@intel.com>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "grovemoisture.h"
#include "upm_utilities.h"
int main()
{
grovemoisture_context dev = grovemoisture_init(14);
int val;
while(1){
if(grovemoisture_get_moisture(dev, &val) != UPM_SUCCESS){
printf("Failed to get any values from the sensor\n");
}
printf("Moisture Value: %d\n", val);
upm_delay(1);
}
grovemoisture_close(dev);
return 0;
}